我在元框中調用ajax調用並返回一些非常簡單的數據。然而,它不斷拋出一個錯誤:WordPress中的ajax/json調用以格式錯誤的JSON結尾
parsererror
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
這裏的Ajax調用:
uid = new Date().getTime();
appReq = jQuery.ajax({
type : 'post',
dataType : 'json',
url : 'admin-ajax.php',
data : { action : 'get_app_sidebars', post_id : currentApp, uid : uid }
});
appReq.done(function(data){
if(data.widgetNum > 0 && data.widgetName != '' && data.widgetBase != '') {
alert(data);
}
else {
// do something here
}
});
appReq.fail(function(jqXHR, textStatus, errorThrown){
alert(textStatus + '\n' + errorThrown);
});
而這裏的PHP:
function get_app_sidebars(){
// get the meta for a custom post type
$meta = $this->get_app_meta($_REQUEST['post_id']);
$result['widgetNum'] = $meta['num_widgets'];
$result['widgetName'] = $meta['widget_name'];
$result['widgetBase'] = $meta['widget_base'];
$result = json_encode($result);
echo $result;
}
的問題出現時,我取回數據:
{"widgetNum":"6","widgetName":"Custom Sidebars","widgetBase":"my-custom-sidebars"}0
好吧,那個當我在螢火蟲中看到這個時,最後的「0」是在迴應中。這讓我瘋狂,我無法弄清楚在發送給瀏覽器的時候,WTF正在使用JSON。如果我修剪0並通過jsonlint運行它,它會通過。我GOOGLE了這一點,只是找不到答案。任何幫助,將不勝感激!
非常好,謝謝! –