javascript AngularJS - 如何禁用 OPTION 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24656488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:10:41  来源:igfitidea点击:

AngularJS - how to disable OPTION request?

javascriptangularjsrequestoptions

提问by redrom

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

我注意到我的 Angular 也在每个 POST 请求之前创建了 OPTIONS 请求。

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

我正在使用自定义 API 服务进行 HTTP 请求处理。

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".

OPTIONS 有什么用吗?我认为这类似于“两台服务器之间的握手”。

Angular version: 1.2.15

角度版本:1.2.15

Thanks for any advice.

感谢您的任何建议。

回答by Quentin

That isn't Angular. It is XMLHttpRequest.

那不是角。它是 XMLHttpRequest。

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

复杂的跨域 HTTP 请求需要预检 OPTIONS 请求,以便浏览器可以确定其他服务器是否会授予 Ajax 请求的权限。

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

如果不能确保您发出的 Ajax 请求很简单,就无法阻止 OPTIONS 请求。

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.)
  • 仅使用 GET、HEAD 或 POST。如果使用 POST 向服务器发送数据,则通过 HTTP POST 请求发送给服务器的数据的 Content-Type 是 application/x-www-form-urlencoded、multipart/form-data 或 text/plain 之一.
  • 不使用 HTTP 请求设置自定义标头(例如 X-Modified 等)

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

除非您想放弃发送 JSON,否则您不能为此使用简单的请求。

回答by TPAKTOPA

I run into a close situation, need to get JSONwith Cross-site HTTP requestsand fixed it by using jQueryrequest, instead of angularjs

我遇到了一个关闭的情况,需要使用跨站点 HTTP 请求获取JSON并使用jQuery请求修复它,而不是angularjs

$.getJSON('http://www.host.com/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 complete");
    });

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

我使用了 Amazon S3 CDN,并且OPTIONS请求不能很好地使用它,所以这救了我。S3服务器的回答是:

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

此分发未配置为允许用于此请求的 HTTP 请求方法。该发行版仅支持可缓存的请求。