javascript 如何调试 jQuery Ajax 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6991306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I debug a jQuery Ajax request?
提问by needhelpwithsumajax
My code is:
我的代码是:
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);
I tested the status code and it came out to a 200 and the error function never goes off, but neither does the success function. Like I said in my comment, it isn't a same-origin policy error. It just stays saying "it isn't working". What's going on here?
我测试了状态代码,结果为 200,错误功能永远不会关闭,但成功功能也没有。就像我在评论中所说的那样,这不是同源策略错误。它只是一直说“它不起作用”。这里发生了什么?
回答by Fernando Fabreti
Try this:
试试这个:
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
回答by jfriend00
Your ajax call is asynchronous. It has not completed yet when your alert at the end runs. Put an actual alert in the success function and you should see your result there.
您的 ajax 调用是异步的。当您最后的警报运行时,它还没有完成。在成功函数中放置一个实际的警报,你应该在那里看到你的结果。
Remember that making the ajax call just starts the asynchronous ajax call and then the rest of your code continues to run. In the code example you posted, that means your alert(test)
call runs right away before the ajax call has completed.
请记住,进行 ajax 调用只会启动异步 ajax 调用,然后其余代码继续运行。在您发布的代码示例中,这意味着您的alert(test)
调用会在 ajax 调用完成之前立即运行。
You can ONLY examine the results of the ajax call from within the success handler itself.
您只能从成功处理程序本身检查 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;
回答by Icarus
To debug these types of things, I find Firebugan indispensable tool. It will show you exactly the response from the server (500 error, 553 error, what have you). You can put break points in your Javascript code and debug it step by step. Firebug works on Firefox.
为了调试这些类型的东西,我发现 Firebug是一个不可或缺的工具。它会准确地显示来自服务器的响应(500 错误、553 错误,你有什么)。您可以在 Javascript 代码中放置断点并逐步调试。Firebug 适用于 Firefox。
For IE, you can use the Developer Tools feature which is similar to Firebug, specially on IE9 which seems more mature than previous versions of the Developer Tools for IE7 or IE8.
对于 IE,您可以使用类似于 Firebug 的开发工具功能,特别是在 IE9 上,它似乎比以前版本的 IE7 或 IE8 开发工具更成熟。
回答by AlphaMale
Move that alert(test) from end into the success function of the ajax call. If it alerts it means code is working else it is not. you can only debug ajax call on its success return.
将该警报(测试)从 end 移动到 ajax 调用的成功函数中。如果它发出警报,则表示代码正在工作,否则就没有。您只能在成功返回时调试 ajax 调用。
var test = "it isn't working";
var response = $.ajax({
type: 'GET',
url: 'jquerydemo.php',
success: function(){
test = "it's working";
alert(test); //It will alert when you ajax call returns successfully.
},
error: function(){
alert("Error detected");
}
}).responseText;
Hope this helps.
希望这可以帮助。
回答by Dhrumil Shah
You can make it like
你可以让它像
var response = $.ajax({
type: 'GET',
url: 'jquerydemo.php',
success: function(){
alert("it's working");
},
error: function(){
alert("Error detected");
}
}).responseText;
This will work....
这将工作......