Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle CORS error code?

Edit: I just realized this is a duplicate of Recommended solution for AJAX, CORS, Chrome & HTTP error codes (401,403,404,500), and he tried the idea I propose at the end. But I can't tell if he succeeded (dud user?), and no one else has posted a solution or even a comment, so I think it's worth fishing for new answers.

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 Code 401

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

Obscured Response Object

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.

like image 599
colllin Avatar asked Nov 02 '14 01:11

colllin


People also ask

How do you handle CORS errors?

Cross-Origin Resource Sharing (CORS) errors occur when a server doesn't return the HTTP headers required by the CORS standard. To resolve a CORS error from an API Gateway REST API or HTTP API, you must reconfigure the API to meet the CORS standard.

How do I fix CORS error on Chrome?

i. Turn OFF the CORS plugin, reload the app, at this time you should still get the errors which are correct. ii. Turn it back ON, reload the app, if the APIs are successful, stop here, no need to proceed to iii.

What is the CORS error code?

If CORS processing fails, the web reverse proxy returns an error response with the CORS error code. Create or modify the corresponding template for error code 38983672 to customize the response returned. By default, IBM Security Verify Access installs a HTML type error template which returns a 400 status code.

How do you fix a CORS issue from the client side?

The only way of resolving a CORS failure is to make sure your server sends the correct response headers.


1 Answers

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.

like image 77
Trott Avatar answered Oct 11 '22 17:10

Trott