Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting an OPTIONS request instead of a GET request?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script> <script> $.get("http://example.com/", function(data) {      alert(data); }); </script> 

it does an OPTIONS request to that URL, and then the callback is never called with anything.

When it isn't cross domain, it works fine.

Shouldn't jQuery just make the call with a <script> node and then do the callback when its loaded? I understand that I won't be able to get the result (since it is cross domain), but that's OK; I just want the call to go through. Is this a bug, or am I doing something wrong?

like image 860
Paul Tarjan Avatar asked Aug 10 '09 18:08

Paul Tarjan


People also ask

Why is an options request sent?

They are necessary when you're making requests across different origins in specific situations. This pre-flight request is made by some browsers as a safety measure to ensure that the request being done is trusted by the server.

Why is there an options request before post?

Prevent sending the post data, if it wont be processed This is the only reason what is valid. Using options request will prevent sending the post data to the server unnecessarily.

What is an option request?

The HTTP OPTIONS method requests permitted communication options for a given URL or server. A client can specify a URL with this method, or an asterisk ( * ) to refer to the entire server. Request has body. No. Successful response has body.

Why is Axios sending options request?

Usually, options request is sent before get automatically by axios, to get some preliminary data before firing get call.


2 Answers

According to MDN,

Preflighted requests

Unlike simple requests (discussed above), "preflighted" requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send. Cross-site requests are preflighted like this since they may have implications to user data. In particular, a request is preflighted if:

  • It uses methods other than GET or POST. Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, or text/plain, e.g. if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted.
  • It sets custom headers in the request (e.g. the request uses a header such as X-PINGOTHER)
like image 183
arturgrigor Avatar answered Nov 09 '22 05:11

arturgrigor


The OPTIONS is from http://www.w3.org/TR/cors/ See http://metajack.im/2010/01/19/crossdomain-ajax-for-xmpp-http-binding-made-easy/ for a bit more info

like image 22
Paul Tarjan Avatar answered Nov 09 '22 06:11

Paul Tarjan