javascript - CORS 错误 - Angularjs $http.post

标签 javascript angularjs cors

我正在尝试执行 $http.post,但此响应给了我此错误。

XMLHttpRequest cannot load http://XXXXXXXXX:2222/server/api/v1/controllers. Response for preflight has invalid HTTP status code 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。

希望你能帮助我,

问候!

最佳答案

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

Access-Control-Allow-Origin: *

关于javascript - CORS 错误 - Angularjs $http.post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42225763/

相关文章:

javascript - 如何在浏览器中绕过 CORS 安全性?

javascript - 在jsTree中,我们如何在替换树后初始选择节点?

javascript - JavaScript 中计算字符串中数字出现的次数

angularjs - ExpressJS + AngularJS : communicate routes and controllers

javascript - AngularStrap typeahead,如何使用事件回调

javascript - express + cors() 无法正常工作

javascript - 内联 jquery 数据赋值

JavaScript 设置和获取 cookie?

AngularJS - 如何在输入时在文本框上应用货币过滤器?

ajax - 使用带有 Nuxt js 的 axios 从 Directions API 获取数据时的跨域读取阻塞 (CORB)