AJAX请求无法获得结果的某种原因
问题描述:
var id = -1;
function addMSG(dataz) {
console.log(dataz);
}
function waitForMsg() {
console.log("start waitmsg");
$.ajax({
url: "chatxml2.php",
type: "POST",
async: true,
cache: false,
timeout: 1000,
data: {id: id, chat: '.filter_input(INPUT_GET,"id").'},
datatype: "xml",
done: function(dataz) {
//addmsg(nera reikia);
addMSG(dataz);
console.log("success");
},
fail: function(XMLHttpRequest, textStatus, errorThrown){
//addmsg("error", textStatus + " (" + errorThrown + ")");
waitForMsg();
console.log("fail");
}
});
}
$(document).ready(function(){
console.log(id);
waitForMsg();
});
由于某种原因,这并没有得到任何结果我不确定ifim使用“data:”正确。 我试图寻找一段时间,但似乎我无法解决这个问题,我对js很不好。AJAX请求无法获得结果的某种原因
PHP脚本正在100%正确地工作,试图用html脚本发布。
答
您的语法错误。 done
和fail
应分别为success
和error
。
所以它应该是:
$.ajax({
url: "chatxml2.php",
type: "POST",
async: true,
cache: false,
timeout: 1000,
data: {id: id, chat: '.filter_input(INPUT_GET,"id").'},
datatype: "xml",
success: function(dataz) {
//addmsg(nera reikia);
addMSG(dataz);
console.log("success");
},
error: function(XMLHttpRequest, textStatus, errorThrown){
//addmsg("error", textStatus + " (" + errorThrown + ")");
console.log("fail");
}
});
如果你想使用递延语法那么这将是:
$.ajax({
url: "chatxml2.php",
type: "POST",
async: true,
cache: false,
timeout: 1000,
data: {id: id, chat: '.filter_input(INPUT_GET,"id").'},
datatype: "xml"
}).done(function() {
addMSG(dataz);
console.log("success");
}).fail(function() {
console.log("fail");
});
答
替代由sucess完成和错误
读取失败有关延迟对象为aviable完成和失败。
'$ .ajax'不会读取选项对象上的'done:'和'fail:'属性。 http://api.jquery.com/jQuery.ajax – 2014-10-16 20:14:24
使用'fixit:function(){alert(“hi”);}'也不会做任何事情。 – 2014-10-16 20:16:18