nee*_*jax 5 javascript ajax jquery
我的代码是:
var test = "it isn't working";
var response = $.ajax({
type: 'GET',
url: 'jquerydemo.php', //This is in the same site as the page calling this request, so it's not a same-domain error.
success: function(){
test = "it's working";
},
error: function(){
alert("Error detected");
}
}).responseText;
alert(test);
Run Code Online (Sandbox Code Playgroud)
我测试了状态代码,它出现了200并且错误函数永远不会消失,但成功函数也没有.就像我在评论中所说的那样,它不是同源政策错误.它只是停留说"它不起作用".这里发生了什么?
Fer*_*eti 12
试试这个:
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
Run Code Online (Sandbox Code Playgroud)
你的ajax调用是异步的.当你的警报结束时,它还没有完成.在成功函数中添加实际警报,您应该在那里看到您的结果.
请记住,进行ajax调用只会启动异步ajax调用,然后其余代码继续运行.在您发布的代码示例中,这意味着您的alert(test)呼叫在ajax调用完成之前立即运行.
您只能从成功处理程序本身中检查ajax调用的结果.
var test = "it isn't working";
var response = $.ajax({
type: 'GET',
url: 'jquerydemo.php', //This is in the same site as the page calling this request, so it's not a same-domain error.
success: function(){
alert("it's working"); // put this here and you will see it
// if the ajax call is successful
},
error: function(){
alert("Error detected");
}
}).responseText;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18539 次 |
| 最近记录: |