【问题标题】:Error with CORS - Angularjs $http.postCORS 出错 - Angularjs $http.post
【发布时间】:2017-07-02 17:16:39
【问题描述】:

我正在尝试做一个 $http.post,但是这个响应给了我这个错误。

XMLHttpRequest 无法加载 http://XXXXXXXXX:2222/server/api/v1/controllers。预检响应包含无效的 HTTP 状态代码 403

我在互联网上读到的所有内容都说这是 CORS 的错误,我试图解决这个问题,但没有成功。

这是我的 $http.post 方法。

        // Set the Content-Type 
        $http.defaults.headers.post["Content-Type"] = "application/json";

        // Delete the Requested With Header
        delete $http.defaults.headers.common['X-Requested-With'];



        var config = {
            method: 'POST',
            headers: {'Content-Type': 'application/json; charset=utf-8'}
         }

        $http.post("http://XXXXXXXXX:2222/server/api/v1/controllers", objToSave , config)
          .then(function successCallback(response) {
            // this callback will be called asynchronously
            // when the response is available
            console.log(response)
          }, function errorCallback(error) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
            console.log(error)
          });

我正在使用 Angular v1.5.5。

希望你能帮助我,

问候!

【问题讨论】:

标签: javascript angularjs cors


【解决方案1】:

chrome 首先发送预检请求以检查服务器是否正确响应。如果服务器正确处理该预检请求,则原始请求将由 chrome 发送。尝试在您的服务器请求处理程序标头中添加以下行。

Access-Control-Allow-Origin: *

【讨论】:

    猜你喜欢
    • 2013-04-07
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 2014-07-04
    相关资源
    最近更新 更多