2011-09-14 41 views
6

我有一個調用php腳本的jquery.ajax例程。 php腳本在Google搜索API上進行查找並將json返回給調用的ajax腳本。jQuery.ajax返回錯誤:出現錯誤消息的意外標記:parseerror?

該腳本工作正常上安裝的99%,但是,在少數,當我打電話:

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

它返回:

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

如何使用JavaScript控制檯我解決此或Chrome開發者工具?代碼存根低於...

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的對象錯誤寫着:

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" 
+0

聽起來像你有不正常的json回來。 ''會暗示可能是一個纏繞json的html標籤? –

+0

哪些區域設置?你爲json返回的URL是什麼?也許你會得到XML呢?請提供更多信息。 – hakre

+0

@hakre:謝謝你檢查了這一點。我剛剛更新了這個問題。 – RegEdit

回答

11

你可能有HTML返回它不應該在JSON。

嘗試console.log(arguments);的警報之前,看看有什麼返回

+0

感謝Neal,當我在插入該控制檯後查看javascript控制檯時,我得到的內容似乎是相同的消息:[Object,「parseerror」,SyntaxError] – RegEdit

+0

@RegEdit - 查看「Object」中的內容 – Neal

+0

+尼爾,現在我有一些線索。剛剛更新了結果的問題。 – RegEdit

0

這告訴你其中的問題是

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" 

最有可能在某些設備中,它的行爲,因爲不同error_reporting設置

以不同的方式

在生產環境中應該關閉錯誤報告,但根據經驗,您的代碼不應該發出任何警告或通知。

在任何情況下,你應該更好地處理尤其是在

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 

這兩個警告的事實,你的變量是不是數組(也許虛假或無效)引起你的錯誤,通常這可以固定在訪問數組之前檢查is_array或者通過總是檢查所有函數的返回值