AngularJS - how to disable OPTION request?

最后都变了- 提交于 2019-12-05 03:51:32

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!