[英]AJAX “success” not called
All of this worked before, I haven't changed anything and now it doesn't. 所有这一切都在以前工作,我没有改变任何东西,现在它没有。 I have two elements named with id="live_title" and id="live_game".
我有两个名为id =“live_title”和id =“live_game”的元素。 AJAX purpose is to update them.
AJAX的目的是更新它们。
My jQuery: 我的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 );
AJAX response as seen in Firebug: Firebug中的AJAX响应:
{"title":"Some title title...","game":"Name of the game"}
PHP Code sending stuff: PHP代码发送内容:
header('Content-Type: application/json');
// creating assoc array here
echo json_encode(array( 'title' => $title, 'game' => $game));
JavaScript console is empty. JavaScript控制台为空。
When I visit ajax/getTitle.php
in my browser, there is no error and displayed is the same thing as normally in Firebug. 当我在浏览器中访问
ajax/getTitle.php
时,没有错误显示与Firebug中的正常情况相同。 Response gives correct header (it's "Content-Type: application/json"). 响应提供正确的标题(它是“Content-Type:application / json”)。
I've added 我已经添加
error : function(data) {
alert(data);
}
To my .ajax()
function. 到我的
.ajax()
函数。 It pops [object Object]
. 它弹出
[object Object]
。 for alert(data.title)
or alert(data['title'])
it's undefined
对于
alert(data.title)
或alert(data['title'])
它是undefined
Try to console your ajax response first and then step forward to further operations 尝试先控制您的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.