开发者

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

开发者 https://www.devze.com 2023-04-05 02:04 出处:网络
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.

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:

erro开发者_开发问答r: 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"


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


This tell you where is the issue is

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"

Most likely in some installations it behaves in a different way because of different error_reporting settings

In production environments error reporting should be off, but as a rule of thumb your code should never emit any warning or notice.

In any case you should handle your errors better especially in

Argument #2 should be an array in /filepath/wp-content/plugins/test/test.php on line 75
Invalid argument supplied for foreach() in /filepath/wpcontent/plugins/test/test.php on line 90

Both these warnings are caused by the fact that your variable is not an array (maybe false or null), usually this can be fixed with an is_array check before accessing the array or by always checking the return value of all functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号