Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
21
rated 0 times [  28] [ 7]  / answers: 1 / hits: 29248  / 10 Years ago, sun, november 2, 2014, 12:00:00

Problem:



  • I send a properly-executed (edit: IMproperly-executed. End of story...) CORS request.

  • The server receives the request and attempts to process it.

  • The server returns an error response, for example a 422 Unprocessable Entity, along with JSON information about the errors. The idea is that my app could receive this error information and handle it appropriately in the UI.

  • The browser blocks my error handler from getting the response content, or even getting the status code.


Showing that the browser received the 401 status code but treated it as a CORS security error:


Status


The response object, showing that my code cannot access the response data (data: "", status: 0):


Obscured


How have other people handled this limitation? My best guess right now is to hijack an HTTP "success" code (2XX) as an error code, and then include the error information in the response. This prevents me from using the ajax error handlers in a normal way, but I'm handling this as a global ajax filter anyway, so this filter would capture the deviant success code and trigger the error handlers instead.


More From » ajax

 Answers
32

The console message indicates that the server isn't sending the required Access-Control-Allow-Origin header when it sends the 401 response code.



You won't be able to use the CORS error handler to inject content into the DOM unless you fix that.



The server is likely sending the header correctly on responses with a 200 response code. It needs to do it for other response codes, though, if you wish to use data from those response codes.



Fix that on the server end before making design compromises on the client side. That may solve your problem straight away.


[#68943] Wednesday, October 29, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
kevonmoisesf questions
Sat, Jan 23, 21, 00:00, 3 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
Wed, Jun 12, 19, 00:00, 5 Years ago
;