Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax returns error: Unexpected token with Error Message: parseerror?

Tags:

jquery

php

I have a jquery.ajax routine that calls a php script. The php script does a lookup on the Google search API and returns json to the calling ajax script.

The script works fine on 99% of installs, however, on a few, when I call:

error: function(jqXHR, textStatus, errorThrown){
alert('HTTP Error: '+errorThrown+' | Error Message: '+textStatus);
}

It returns:

HTTP Error: SyntaxError: Unexpected token < | Error Message: parsererror

How can I troubleshoot this using javascript console or chrome developer tools? Code stub is below...

var result='';
jQuery.ajax
({
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    url: <?php  echo '"' .plugins_url('/script.php', __FILE__); ?>?Query="+ jQuery('#search_keyword').val(),
    success: function(data)
    {       
        //do something with results
    },

    error: function(jqXHR, textStatus, errorThrown){
        console.log(arguments);
        alert('HTTP Error: '+errorThrown+' | Error Message: '+textStatus);
        return;
    }
});

UPDATE: Console.log's OBJECT error reads:

responseText: "<br />↵<b>Warning</b>:  array_map() [<a href='function.array-map'>function.array-map</a>]: Argument #2 should be an array in <b>/filepath/wp-content/plugins/test/test.php</b> on line <b>75</b><br />↵<br />↵<b>Warning</b>:  Invalid argument supplied for foreach() in <b>/filepath/wp-content/plugins/test/test.php</b> on line <b>90</b><br />↵No Records Returned. Search may be down. Wait a few minutes"
like image 233
RegEdit Avatar asked Sep 14 '11 17:09

RegEdit


1 Answers

You probably have HTML returning where it was not supposed to for the JSON.

Try console.log(arguments); before the alert to see what is returned

like image 159
Naftali Avatar answered Oct 13 '22 00:10

Naftali