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

标签 javascript angularjs request http-options-method

我注意到我的 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/

相关文章:

javascript - Angular 选择选项忽略第一个数组值

javascript - 使用 php 运行在提交 from 后编辑 css 的 JavaScript?

javascript - 使用或不使用 string.slice 对字符串中的字符进行计数

javascript - 如何编写仅允许数字和 "("、 ")"、 "-"的正则表达式

javascript - CORS 在 jquery 中工作正常但在 angularjs 中不工作

android - FacebookSDK Request.newMeRequest 从未被调用

javascript - 当我使用Web SQL时,我遇到了麻烦

javascript - $injector 在我的测试中不起作用,为什么?

javascript - 如何使用 GraphQL 突变减少请求数?

python flask请求钩子(Hook)