简体   繁体   English

如何调试ajax请求错误?

[英]How to debug ajax request error?

I'm trying to get a JSON, hosted in a local server here, but for some reason it doesn't work. 我正在尝试获取一个JSON,在这里托管在本地服务器上,但由于某种原因它不起作用。 If i hit my url on the browser it returns the Json correctly and If I use another URL, I do get some data. 如果我在浏览器上点击我的网址,它会正确返回Json,如果我使用其他网址,我会获得一些数据。

My original getJson method: 我原来的getJson方法:

getJson: 的getJSON:

$.getJSON(url, 

                    function(data) {
                        console.log('got something'); 
                        alert(data);    

                    });

Now I want to see exactly what's wrong. 现在我想看看究竟出了什么问题。 I've rewritten it as a Ajax request, and on my error callback jqxhr gives me a lot of things that I don't understand. 我把它重写为Ajax请求,在我的错误回调jqxhr给了我很多我不理解的东西。 Can i get more details about this error ? 我能获得有关此错误的更多详细信息吗?

TextStatus value is "error" TextStatus值是"error"

errorThrown is null errorThrownnull

    $.ajax({
                  url: url,
                  dataType: 'json',
                  success:  function(data) {
                        console.log('got something'); 
                        alert(data);    
                        },
                error: function(jqxhr,textStatus,errorThrown)
                    {
                        console.log(jqxhr);
                            console.log(textStatus);
                            console.log(errorThrown);                               

                            for (key in jqxhr)
                                alert(key + ":" + jqxhr[key])                                                                 
                            for (key2 in textStatus)
                                alert(key + ":" + textStatus[key])
                            for (key3 in errorThrown)
                                alert(key + ":" + errorThrown[key])

                   //<--- All those logs/alerts, don't say anything helpful, how can I understand what error is going on? ---->

                }});

Last, the json i'm supposed to get back is 最后,我应该回来的json是

[{"message": "login failed"}]

And I can't use firebug, chrome console, or any other dev tools because this is a mobile app, developed using Phonegap! 我不能使用firebug,chrome控制台或任何其他开发工具,因为这是一个使用Phonegap开发的移动应用程序!

I am gonna put my comments into answer form, its not a solution to your problem but its a good way to troubleshoot various return issues with jQuery Ajax. 我将把我的评论放在答案表单中,它不是解决问题的方法,但它是解决jQuery Ajax的各种返回问题的好方法。

When you are requesting a certain datatype in this case dataType: 'json' you are requesting a specific type of data. 当您在这种情况下请求某种数据类型dataType: 'json'您正在请求特定类型的数据。 If that specific dataType is not returned it will pass to the error function. 如果未返回该特定dataType,则将传递给错误函数。

If your error message isnt providing information that will explain your problem then try requesting a dataType that is more friendly. 如果您的错误消息未提供可解释您的问题的信息,请尝试请求更友好的dataType。 This way you can see what your request is actually returning. 这样您就可以看到您的请求实际返回的内容。 If its data other than your requested dataType then that is your issue and you can work it out from there. 如果它的数据不是你请求的dataType,那么这就是你的问题,你可以从那里开始工作。

dataType like html or text will be very open to give you the exact data returned from the requested url and you can solve your issues from there. htmltext这样的dataType将非常开放,可以为您提供从请求的URL返回的确切数据,您可以从那里解决您的问题。

Hope it helps a little. 希望它有所帮助。

jqxhr - XrayWrapper jqxhr - XrayWrapper
textStatus - represents the text representation of Https response status (eg. success, error) textStatus - 表示Https响应状态的文本表示(例如,成功,错误)
errorThrown - XMLHttpRequest errorThrown - XMLHttpRequest

jqxhr.status - gives the Http response code such as 200, 404, 500 so on.. jqxhr.status - 给出Http响应代码,例如200,404,500等等。

Use this code snippet to print all the key in the json object 使用此代码段打印json对象中的所有键

var output = '';
for (property in object) {
  output += property + ': ' + object[property]+'; ';
}
console.log(output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM