jQuery Ajax - 如何获取错误的响应数据 [英] jQuery Ajax - how to get response data in error

查看:1030
本文介绍了jQuery Ajax - 如何获取错误的响应数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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



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



编辑:



1)这是我的JS代码:

 函数寄存器(userName,password){
var postData = {};
postData [userName] = userName;
postData [password] =密码;

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

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

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



我做错了什么?

解决方案

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



该页面位于以下URL:www.mydomain.com/register

REST api位于以下URL:server.mydomain.com/rest



看起来这种POST并不是那么简单。

我会搜索更多信息以更好地理解这个问题(如果你有更多信息请与我分享)。



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


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.).

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"}.

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".

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?

Edit:

1) Here is my JS code:

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.

What am I doing wrong?

解决方案

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

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).

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

这篇关于jQuery Ajax - 如何获取错误的响应数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆