简体   繁体   English

如何使用 jQuery.Ajax 访问 JSON 响应以获取 400 错误?

[英]How do I access the JSON response with jQuery.Ajax for a 400 error?

In jQuery, I'm submitting forms to the server.在 jQuery 中,我将表单提交到服务器。 When there is a validation error, I receive a 400 error from the server, and the body of the document is valid JSON.当出现验证错误时,我会收到来自服务器的 400 错误,并且文档正文是有效的 JSON。 I would like to know how to access the data returned from the server.我想知道如何访问从服务器返回的数据。

My .error callback function on the jQuery.Ajax object is never called, so I'm using .statusCode{400} function.我在 jQuery.Ajax 对象上的 .error 回调函数从未被调用,所以我使用 .statusCode{400} 函数。 This runs just fine, however none of the arguments contain the response body.这运行得很好,但是没有一个参数包含响应正文。

Some browsers' XHR implementations refuse to provide the response body if the HTTP status is not 2xx.如果 HTTP 状态不是 2xx,一些浏览器的 XHR 实现会拒绝提供响应正文。 What I've had to resort to in API design where I couldn't control my clients was to do something like always return 200 and indicate success/failure/status in some other way (eg, as a top-level attribute in the JSON response).在 API 设计中我不得不求助于我无法控制我的客户的是做一些事情,比如总是返回 200 并以其他方式指示成功/失败/状态(例如,作为 JSON 中的顶级属性回复)。

I try to get json response with a status 400, and it works on IE7,8 and 9, Firefox and Chrome (Safari not tested).我尝试获得状态为 400 的 json 响应,它适用于 IE7、8 和 9、Firefox 和 Chrome(Safari 未测试)。

...
error: function(xhr) {
   error(xhr.responseText);
}
...

I have had no trouble using statusCode in the callback, However, statusCode as a callback function Does NOT return any data.我在回调中使用 statusCode 没有问题,但是,作为回调函数的 statusCode返回任何数据。

jQuery.Ajax jQuery.Ajax

I think you should try a different approach on how to handle validation errors in the server side, to return status code 200, but with a parameter like "error_count" and go from there.我认为您应该尝试一种不同的方法来处理服务器端的验证错误,返回状态代码 200,但使用“error_count”之类的参数并从那里开始。

can you post some of the code you are using (just $.ajax you are using...) ?你能发布一些你正在使用的代码吗(只是你正在使用的 $.ajax ......)?

For me (Chrome), responseText property of the xhr response contains only "BAD REQUEST" when statusCode is 400. Thanks Steven for your response;对于我(Chrome),当 statusCode 为 400 时,xhr 响应的 responseText 属性仅包含“BAD REQUEST”。感谢 Steven 的回复; I was struggling with this since in Postman I could get the json data I was returning from my REST controller, but couldn't find it on the jQuery Ajax response.我一直在为此苦苦挣扎,因为在 Postman 中我可以获取从 REST 控制器返回的 json 数据,但在 jQuery Ajax 响应中找不到它。

"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 属性提供。

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

If you're specifying your callback with statusCode as mentioned in the question:如果您使用问题中提到的statusCode指定您的回调:

"If the request is successful, the status code functions take the same parameters as the success callback; if it results in an error (including 3xx redirect), they take the same parameters as the error callback. " (jqXHR, textStatus, errorThrown) “如果请求成功,状态码函数采用与成功回调相同的参数;如果导致错误(包括3xx重定向),它们采用与错误回调相同的参数。 ”(jqXHR,textStatus,errorThrown)

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

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