jQuery Ajax - 如何获取错误的响应数据

时间:2022-10-08 23:59:32

I have a simple web application. I've created the server REST API so it will return a response with HTTP code and a JSON (or XML) object with more details: application code (specific to scenario, message that describe what happened etc.).

我有一个简单的Web应用程序。我已经创建了服务器REST API,因此它将返回带有HTTP代码的响应和带有更多详细信息的JSON(或XML)对象:应用程序代码(特定于场景,描述发生的事件的消息等)。

So, for example if a client send a Register request and the password is too short, the response HTTP code will be 400 (Bad Request), and the response data will be: {appCode : 1020 , message : "Password is too short"}.

因此,例如,如果客户端发送注册请求且密码太短,则响应HTTP代码将为400(错误请求),响应数据将为:{appCode:1020,消息:“密码太短” }。

In jQuery I'm using the "ajax" function to create a POST request. When the server returns something different from HTTP code 200 (OK), jQuery defines it as "error".

在jQuery中,我使用“ajax”函数来创建POST请求。当服务器返回与HTTP代码200(OK)不同的内容时,jQuery将其定义为“错误”。

The error handler can get 3 parameters: jqXHR, textStatus, errorThrown. Ho can I get the JSON object that sent by the server in error case?

错误处理程序可以获得3个参数:jqXHR,textStatus,errorThrown。我可以获取服务器在错误情况下发送的JSON对象吗?

Edit:

编辑:

1) Here is my JS code:

1)这是我的JS代码:

function register (userName, password) {
    var postData = {};
    postData["userName"] = userName;
    postData["password"] = password;

    $.ajax ({
        dataType: "json",
        type: "POST",
        url: "<server>/rest/register",
        data: postData,
        success: function(data) {
            showResultSucceed(data);
            hideWaitingDone();
        },
        error: function (jqXHR, textStatus, errorThrown) {

            showResultFailed(jqXHR.responseText);
            hideWaitingFail();
        }
    })
}

2) When looking at Firebug console, it seems like the response is empty. When invoking the same request by using REST testing tool, I get a response with JSON object it it.

2)在查看Firebug控制台时,响应似乎是空的。当使用REST测试工具调用相同的请求时,我得到了一个带有JSON对象的响应。

What am I doing wrong?

我究竟做错了什么?

3 个解决方案

#1


32  

Here's an example of how you get JSON data on error:

以下是如何获取有关错误的JSON数据的示例:

$.ajax({
    url: '/path/to/script.php',
    data: {'my':'data'},
    type: 'POST'
}).fail(function($xhr) {
    var data = $xhr.responseJSON;
    console.log(data);
});

From the docs:

来自文档:

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

如果指定了json,则在作为对象传递给成功处理程序之前,使用jQuery.parseJSON解析响应。解析的JSON对象通过jqXHR对象的responseJSON属性提供。

Otherwise, if responseJSON is not available, you can try $.parseJSON($xhr.responseText).

否则,如果responseJSON不可用,您可以尝试$ .parseJSON($ xhr.responseText)。

#2


25  

directly from the docs

直接来自文档

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader()

自jQuery 1.5起,$ .ajax()返回的jQuery XMLHttpRequest(jqXHR)对象是浏览器的本机XMLHttpRequest对象的超集。例如,它包含responseText和responseXML属性,以及getResponseHeader()

so use the jqXRH argument and get the responseText property off it.

所以使用jqXRH参数并从中获取responseText属性。

In the link above, look for the section entitled

在上面的链接中,查找标题为的部分

The jqXHR Object

jqXHR对象

#3


-3  

After spending so much time on this problem, I found the problem.

在这个问题上花了这么多时间后,我发现了问题。

The page is under the URL: www.mydomain.com/register
The REST api is under the URL: server.mydomain.com/rest

该页面位于以下URL:www.mydomain.com/register REST api位于以下URL:server.mydomain.com/rest

Seems like this kind of POST is not so simple.
I'm going to search more information to understand this issue better (if you have more information please share it with me).

好像这种POST并不那么简单。我将搜索更多信息以更好地了解此问题(如果您有更多信息,请与我分享)。

When putting the REST API under www.mydomain.com/rest - everything is working fine.

将REST API放在www.mydomain.com/rest下 - 一切正常。

#1


32  

Here's an example of how you get JSON data on error:

以下是如何获取有关错误的JSON数据的示例:

$.ajax({
    url: '/path/to/script.php',
    data: {'my':'data'},
    type: 'POST'
}).fail(function($xhr) {
    var data = $xhr.responseJSON;
    console.log(data);
});

From the docs:

来自文档:

If json is specified, the response is parsed using jQuery.parseJSON before being passed, as an object, to the success handler. The parsed JSON object is made available through the responseJSON property of the jqXHR object.

如果指定了json,则在作为对象传递给成功处理程序之前,使用jQuery.parseJSON解析响应。解析的JSON对象通过jqXHR对象的responseJSON属性提供。

Otherwise, if responseJSON is not available, you can try $.parseJSON($xhr.responseText).

否则,如果responseJSON不可用,您可以尝试$ .parseJSON($ xhr.responseText)。

#2


25  

directly from the docs

直接来自文档

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader()

自jQuery 1.5起,$ .ajax()返回的jQuery XMLHttpRequest(jqXHR)对象是浏览器的本机XMLHttpRequest对象的超集。例如,它包含responseText和responseXML属性,以及getResponseHeader()

so use the jqXRH argument and get the responseText property off it.

所以使用jqXRH参数并从中获取responseText属性。

In the link above, look for the section entitled

在上面的链接中,查找标题为的部分

The jqXHR Object

jqXHR对象

#3


-3  

After spending so much time on this problem, I found the problem.

在这个问题上花了这么多时间后,我发现了问题。

The page is under the URL: www.mydomain.com/register
The REST api is under the URL: server.mydomain.com/rest

该页面位于以下URL:www.mydomain.com/register REST api位于以下URL:server.mydomain.com/rest

Seems like this kind of POST is not so simple.
I'm going to search more information to understand this issue better (if you have more information please share it with me).

好像这种POST并不那么简单。我将搜索更多信息以更好地了解此问题(如果您有更多信息,请与我分享)。

When putting the REST API under www.mydomain.com/rest - everything is working fine.

将REST API放在www.mydomain.com/rest下 - 一切正常。