Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - how to disable OPTION request?

I noticed that my Angular is creating OPTIONS request also before each POST request.

I'm using custom API Service for HTTP request handling.

app.service('ApiService', function ($http) {

/**
 * Process remote POST request to give URL with given params
 * @param {String} url
 * @param {String} POST params
 * @return {JSON}  response from server
 */
this.doHttpRequest = function (type, url, params) {
    return $http({
        method: type,
        url: url,
        data: params,
        timeout: 5000,
        headers: {
            "Content-Type": "application/json",
        }
    });
}

}); 

Question is:

How can i disable it (which config values put where)?

Is OPTIONS good for something? I think that is it something like "Handshake between 2 servers".

Angular version: 1.2.15

Thanks for any advice.

like image 254
redrom Avatar asked Jul 09 '14 14:07

redrom


People also ask

Why is options request sent?

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. Meaning the server understands that the method, origin and headers being sent on the request are safe to act upon.

What is Preflight options request?

A CORS preflight request is a CORS request that checks to see if the CORS protocol is understood and a server is aware using specific methods and headers. It is an OPTIONS request, using three HTTP request headers: Access-Control-Request-Method , Access-Control-Request-Headers , and the Origin header.

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.


1 Answers

That isn't Angular. It is XMLHttpRequest.

Complex cross-origin HTTP requests require a pre-flight OPTIONS request so the browser can find out if the other server will grant permission for the Ajax request.

Short of making sure the Ajax request you are making is simple, there is no way to prevent the OPTIONS request.

A simple request:

  • Only uses GET, HEAD or POST. If POST is used to send data to the server, the Content-Type of the data sent to the server with the HTTP POST request is one of application/x-www-form-urlencoded, multipart/form-data, or text/plain.
  • Does not set custom headers with the HTTP Request (such as X-Modified, etc.)

Unless you wish to give up sending JSON, you can't use a simple request for this.

like image 155
Quentin Avatar answered Oct 20 '22 00:10

Quentin