最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS - how to disable OPTION request? - Stack Overflow

programmeradmin9浏览0评论

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.

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.

Share Improve this question edited Jul 18, 2022 at 2:59 Daniel Widdis 9,13113 gold badges48 silver badges68 bronze badges asked Jul 9, 2014 at 14:30 redromredrom 11.6k33 gold badges165 silver badges268 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 19

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.

I run into a close situation, need to get JSON with Cross-site HTTP requests and fixed it by using jQuery request, instead of angularjs

$.getJSON('http://www.host./test.json', '', function (data) {
    if (meta) {
        console.log("jq success :" + JSON.stringify(data));
    }
    console.log("jq success, but empty JSON");
})
    .done(function () {
        console.log("jq second success");
    })
    .fail(function (data) {
        console.log("jq fail " + JSON.stringify(data));
    })
    .always(function () {
        console.log("jq plete");
    });

I used Amazon S3 CDN, and OPTIONS requests didn't work well with it, so this saved me. The answer from S3 server was:

This distribution is not configured to allow the HTTP request method that was used for this request. The distribution supports only cachable requests.

发布评论

评论列表(0)

  1. 暂无评论