Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX in Chrome sending OPTIONS instead of GET/POST/PUT/DELETE?

I am working on an internal web application at work. In IE10 the requests work fine, but in Chrome all the AJAX requests (which there are many) are sent using OPTIONS instead of whatever defined method I give it. Technically my requests are "cross domain." The site is served on localhost:6120 and the service I'm making AJAX requests to is on 57124. This closed jquery bug defines the issue, but not a real fix.

What can I do to use the proper http method in ajax requests?

Edit:

This is in the document load of every page:

jQuery.support.cors = true; 

And every AJAX is built similarly:

var url = 'http://localhost:57124/My/Rest/Call'; $.ajax({     url: url,     dataType: "json",     data: json,     async: true,     cache: false,     timeout: 30000,     headers: { "x-li-format": "json", "X-UserName": userName },     success: function (data) {         // my success stuff     },     error: function (request, status, error) {         // my error stuff     },     type: "POST" }); 
like image 830
Corey Ogburn Avatar asked Feb 14 '14 15:02

Corey Ogburn


People also ask

Does AJAX use Get or POST?

GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

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.

Can AJAX use POST?

post() makes Ajax requests using the HTTP POST method. The basic syntax of these methods can be given with: $. get(URL, data, success); —Or— $.

Why do we use processData in AJAX?

processData. If set to false it stops jQuery processing any of the data. In other words if processData is false jQuery simply sends whatever you specify as data in an Ajax request without any attempt to modify it by encoding as a query string.


1 Answers

Chrome is preflighting the request to look for CORS headers. If the request is acceptable, it will then send the real request. If you're doing this cross-domain, you will simply have to deal with it or else find a way to make the request non-cross-domain. This is why the jQuery bug was closed as won't-fix. This is by design.

Unlike simple requests (discussed above), "preflighted" requests first send an HTTP request by the OPTIONS method 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, HEAD 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 134
Dark Falcon Avatar answered Oct 08 '22 13:10

Dark Falcon