vaske
vaske

Reputation: 9542

jQuery CORS turns GET into OPTIONS call

This is a sample code which I use

    $.ajax({
        type: "get",
        url: "http://myserver.com",
        beforeSend: function (xhr, data) {
            xhr.setRequestHeader("Authorization", "xxxx");
        }
});

when I inspect this call in firebug I see OPTIONS and also I can't see Authorization code in request header. CORS headers has been added to server side.

    response.addHeader("Access-Control-Allow-Headers", "origin, content-type, accept, x-requested-with");
    response.addHeader("Access-Control-Max-Age", "60"); 
    response.addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
    response.addHeader("Access-Control-Allow-Origin", "*");     
    response.addHeader("Access-Control-Allow-Credentials", "true"); 

Idea is to use pure jQuery if it's possible to perform such a queries.

Upvotes: 0

Views: 1324

Answers (1)

Ray Nicholus
Ray Nicholus

Reputation: 19890

The preflight (OPTIONS) is occurring due to the fact that you are sending a cross-origin ajax request AND specifying an Authorization header with this GET request.

Also (this is not causing an issue) I would suggest removing the contentType option. This doesn't make sense in the context of a GET request. A GET request should not have any content. All data should be included in the query string or, possibly, headers.

The Authorization header will not be sent with the OPTIONS. You must acknowledge it server-side, and then the browser will send the underlying GET. Read more about CORS at https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS.

Upvotes: 3

Related Questions