0

I am using codeigniter as backend and ANGULAR 4 As a REST Based Frontend Client.The problem is every request is sent twice ONE is using OPTION and other is using GET or POST.

Screenshot of Console Network tab.

https://i.stack.imgur.com/q3GqL.jpg

I got this in console for each of the every other API request, Once I have First successfull login api. How to stop the extra OPTION request because my server is not coded to handle this request. I know it has to do something with preflight request option and I am not sure how to solve it.

Update

due to setting the below custom header, I am getting triggred with additional OPTIONS request.

         this.headers.append('sessionname','7datfaaj6slo7p7htubtutn970l20lfd'); 

Now , I have enabled the CORS on serverside and put this condition to give correct output for OPTIONS request.

       $method = $_SERVER['REQUEST_METHOD']; if($method == "OPTIONS") { die(); }

So it is working fine, But now my question is it perfect way to handel this annoying OPTIONS request?

  • enable cors on your server side http://www.competa.com/blog/enable-cors-origin-resource-sharing-on-codeigniter-rest-server/ – Maninderpreet Singh Jan 30 '18 at 09:26
  • You need to provide https://stackoverflow.com/help/mcve. – Peter Jan 30 '18 at 09:26
  • This is due to Cross-Origin Resource Sharing (CORS), please read this https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS – Ayman El Temsahi Jan 30 '18 at 09:27
  • I think my situation is similar to https://stackoverflow.com/questions/30920422/how-to-handle-custom-headers-with-cors-pre-flight-request-ajax-codeigniter Now using the below check before every constructor I am able to get correct response code for every Options request. This OPTION tag was triggered by additional custom header. – Talk is Cheap Show me Code Jan 30 '18 at 09:37

2 Answers2

1

As the answer below states, it is not the REST client that sends the OPTIONS, it is the browser, so you better update your server-side code to suit the needs of the browser.

OPTIONS Explanation

Boanta Ionut
  • 402
  • 8
  • 24
-1

You can use this plugin : CORS Chrome Extension

Abhilash Rathod
  • 563
  • 6
  • 11