gpt4 book ai didi

javascript - AngularJS - 如何禁用 OPTION 请求?

转载 作者:可可西里 更新时间:2023-11-01 02:54:38 28 4
gpt4 key购买 nike

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

我正在使用自定义 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",
}
});
}

});

问题是:

我如何禁用它(哪些配置值放在哪里)?

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

Angular 版本:1.2.15

感谢您的任何建议。

最佳答案

那不是 Angular。它是 XMLHttpRequest。

复杂的跨源 HTTP 请求需要 a pre-flight OPTIONS request这样浏览器就可以知道其他服务器是否会为 Ajax 请求授予权限。

无法确保您发出的 Ajax 请求是 simple , 没有办法阻止 OPTIONS 请求。

一个简单的请求:

  • 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.)

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

关于javascript - AngularJS - 如何禁用 OPTION 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24656488/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com