[英]AJAX “success” not called
所有這一切都在以前工作,我沒有改變任何東西,現在它沒有。 我有兩個名為id =“live_title”和id =“live_game”的元素。 AJAX的目的是更新它們。
我的jQuery:
var tytul = function getTitle(){
$.ajax({
type : 'GET',
dataType: 'json',
url : 'ajax/getTitle.php',
success : function(data) {
for (var prop in data) {
$('#live_'+prop).html(data[prop]);
}
}
});
}
setInterval( tytul, 10000 );
Firebug中的AJAX響應:
{"title":"Some title title...","game":"Name of the game"}
PHP代碼發送內容:
header('Content-Type: application/json');
// creating assoc array here
echo json_encode(array( 'title' => $title, 'game' => $game));
JavaScript控制台為空。
當我在瀏覽器中訪問ajax/getTitle.php
時,沒有錯誤顯示與Firebug中的正常情況相同。 響應提供正確的標題(它是“Content-Type:application / json”)。
我已經添加
error : function(data) {
alert(data);
}
到我的.ajax()
函數。 它彈出[object Object]
。 對於alert(data.title)
或alert(data['title'])
它是undefined
嘗試先控制您的ajax響應,然后再前進到進一步的操作
var tytul = function getTitle(){
$.ajax({
type : 'GET',
dataType: 'json',
url : 'ajax/getTitle.php',
success : function(data) {
//if successfull
console.log(data);
},
error:function(data){
//if error
console.log(data);
}
});
}
setInterval( tytul, 10000 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.