Joe
Joe

Reputation: 277

Debugging AJAX requests

I am currently trying to figure out how I can find out what errors are in my php script.

I have made a small script which an error on purpose. Using the chrome console I get...

Uncaught SyntaxError: Unexpected token < 

However, normally PHP errors would point you to the line in which the error occurs.

Here is my AJAX Request.

var myData = "Hello";

        $.ajax({ 
            type: "GET",
            url: 'test.php',
            datatype: 'json',
            data: { data: myData},
            success: function(output) {
                var result = $.parseJSON(output);
                for(var i=0; i<result.length; i++){
                    console.log(result[i]);
                }
            }
        });

And my PHP script with the syntax error

$data = ($_GET['data']);
echoo boom;
echo json_encode(array($data, "test"));

As you can see, line 2 is the error but the error in my console gives me no information as such.

Thanks

Upvotes: 0

Views: 132

Answers (4)

newBee
newBee

Reputation: 1319

$.ajax({ 
    type: "GET",
    url: 'test.php',
    datatype: 'json',
    data: {},
    success: function(output) {
        result = output;
        for(var i=0; i < result.length; i++){
            console.log(result[i]);
        }
    }
});

By defining the dataType as json the output is already parsed. Parsing the already parsed 'output' again led to the error. So it was a client side / javascript error.

ALso note that - in case an error occurs - the actual error message is parsed too. So you get an array containing the "fragmented" error message from the server ;)

Upvotes: 2

David
David

Reputation: 218828

Use the debugging tools in your browser. Specifically look for a tab or set of functionality for monitoring network requests. This will show you the actual request and response to and from the server in your AJAX.

What's happening here is your PHP code is generating an error, which results in returning some kind of HTML to the browser (possibly showing the error message). However, look at what you're doing with the response:

var result = $.parseJSON(output);

If output is HTML and not JSON, then this code will fail with an error. Since JSON syntax doesn't use < but HTML does, I would expect you to get the error:

Uncaught SyntaxError: Unexpected token <

Upvotes: 0

mlewis54
mlewis54

Reputation: 2380

When the error is server side you typically only get an error message (maybe 500 or Server Error) on the client side which is very non-specific. You must debug on the web server. Once I see such an error I check out the web server's error log file (which will show both compile and run time errors).

Upvotes: 1

Xanarus
Xanarus

Reputation: 775

Please write :

$data = $_GET['data'];
echo boom;
echo json_encode(array($data, "test"));

Instead of :

$data = ($_GET['data']);
echoo boom;
echo json_encode(array($data, "test"));

And write :

$.ajax({
                type: "GET",
                url: "test.php",
                timeout: 6000,
                success: function(data) {


                    json = $.parseJSON(data);

                    for (var i = 0; i <= json.length-120; i++) {


                        console.log(json[i]);


                    }



                }
});

Upvotes: 0

Related Questions