0

I have a problem with the communication between AJAX and PHP. I've already tried all these pieces of code from related questions and answers but nothing worked. This code should recieve a JSON string from a PHP file on the same server and display the values in the HTML area.

$(document).ready(function(){
    setInterval(ajaxcall, 1000);
});

function ajaxcall(){
var data = $('#signup_form').serialize();

    $.ajax({
        type: "GET",
        url: "test.php",
        data: data,
        dataType: 'json',
        success: function (data) {
            window.console.log("Success");
            document.getElementById("count").innerHTML = $('#time').html(data.time);
    },error: function alert(){"error";}
});}

When I execute the script in my browser (with debug mode) then nothing happens. I assume this implies a problem with the 'success' method.

Here is a part of my PHP code (for testing):

for($i = 0; $i < 50; $i++){
    sleep(1);
    return_json($argv[1]);
}

//return all values for html/javascript
function return_json($a){
    $json = array('time' => $a);
    echo json_encode($json);
}

I hope some of you can help me :)

5
  • 1
    Does the ajax call ctually return something? Use firebug or something simular to check hwat your ajax call is returning
    – Naruto
    Jun 14, 2017 at 14:08
  • with the 'window.console' method i should be able to see something every second, but nothing is written in the console.
    – Slice18
    Jun 14, 2017 at 14:10
  • 1
    where you have set up variable from $_GET? $argv is used for cli I think...for ajax use superglobal $_GET Jun 14, 2017 at 14:14
  • even if it works, this is likely to output lots of disparate JSON objects or arrays. not sure this will form a valid response on the client. Collect all your data into one place in the PHP and then do one json_encode command on all of it, to form a coherent object/array to output. Secondly, you're not reading the input data in PHP - you need to read the contents of the $_GET array (and validate it!!!). Lastly error: function alert(){"error";} should be error: function () { alert("error");} I think (although it could also take the status code/message as well and log/alert them).
    – ADyson
    Jun 14, 2017 at 14:45
  • The $argv is just the input for the PHP script. Thanks for the hint with the error part, this is now working. But it still doesn't get data from my PHP.
    – Slice18
    Jun 20, 2017 at 6:53

2 Answers 2

0

Is the function ajaxcall() really being called? Try to add a console.log call at its begining, eg

function ajaxcall(){
   var data = $('#signup_form').serialize();
   console.log(' form data = %o', data)

   //...

There is another possible issue the function error: function alert(){"error";} does nothing visible. If your server script is returning an error you will see nothing in this case.

Change it to something like

error: function(){ alert("error"); }

0

You should add the content type to your response header:

//this line
header('Content-type: application/json');

for($i = 0; $i < 50; $i++){
    sleep(1);
    return_json($argv[1]);
}

//return all values for html/javascript
function return_json($a){
    $json = array('time' => $a);
    echo json_encode($json);
}

Further reading and a more detailed explanation here: jQuery $.ajax request of dataType json will not retrieve data from PHP script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.