使用AngularJS的$ http POST请求将我的数据发布到服务器但失败并显示错误消息“Access-Control-Allow-Origin不允许”

我正在搜索互联网,寻找找到使用AngularJS将数据发布到服务器的正确方法的方法。

当我使用Angular JS执行$ http POST请求时,它将我的数据发布到服务器但是失败并显示错误消息“Origin http://localhost:8000不允许由Access-Control-Allow-Origin”,无论是否我用“file:///”试试,结果仍然一样。

我尝试了互联网上提到的所有补救措施,包括

  app.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; }]); 

并遵循本文中的大部分说明,但它们对我不起作用。

我还设置了Access-Control-Allow-Origin: *access-control-allow-headers: Origin, X-Requested-With, Content-Type, Accept, access_token, Origin, X-Requested-With, Content-Type, Accept, access_token服务器端方法,但我仍然得到相同的错误,以下是我的代码

 var URL = "https://myURL"; var app = angular.module('myApp', []); app.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With']; }]); app.controller('UserController', function($scope, $http){ $scope.user = {}; $scope.createUser = function () { $http({ url: URL, data: $scope.user, method: "POST", headers: { "Content-Type": "application/json", "access_token": "sometoken" } }).success(function(response){ $scope.response = response; alert('ok'); }).error(function(error){ $scope.error = error; alert('error'); }); } }); 

我不知道我在这里做错了什么,请帮帮我。

编辑:

以下是我的预检标题

 Request URL:https://myURL Request Method:OPTIONS Status Code:200 OK Request Headersview source :host:someapp.appspot.com :method:OPTIONS :path:somepath :scheme:https :version:HTTP/1.1 accept:*/* accept-encoding:gzip,deflate,sdch accept-language:en-US,en;q=0.8 access-control-request-headers:accept, origin, access_token, content-type access-control-request-method:POST dnt:1 origin:http://localhost:8000 referer:http://localhost:8000/samplepost.html user-agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 Response Headersview source access-control-allow-headers:Origin, X-Requested-With, Content-Type, Accept, access_token access-control-allow-origin:* alternate-protocol:443:quic content-length:0 content-type:text/html date:Mon, 16 Sep 2013 17:58:03 GMT server:Google Frontend status:200 OK version:HTTP/1.1 

以下是我的POST标题:

 Request URL:someURL Request Headersview source Accept:application/json, text/plain, */* access_token:dsfdsfds Content-Type:application/json Origin:http://localhost:8000 Referer:http://localhost:8000/samplepost.html User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36 Request Payloadview source {name:adasds, email:sdsfd@gmail.com, phone:325325325, comments:fddsfsdfsd} comments: "fddsfsdfsd" email: "sdsfd@gmail.com" name: "adasds" phone: "325325325" 

Firefox请求标头和响应标头:

 Response Headersview Access-Control-Allow-Orig... *, * Alternate-Protocol 443:quic Cache-Control private Content-Encoding gzip Content-Length 136 Content-Type application/json Date Mon, 16 Sep 2013 18:30:17 GMT Server Google Frontend Vary Accept-Encoding X-Firefox-Spdy 3 access-control-allow-head... Origin, X-Requested-With, Content-Type, Accept, access_token, Origin, X-Requested-With, Content-Type, Accept, access_token Request Headersview source Accept application/json, text/plain, */* Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Cache-Control no-cache Connection keep-alive Content-Length 100 Content-Type application/json; charset=UTF-8 Host someapp.appspot.com Origin null Pragma no-cache User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0 access_token dsfdsfds 

我有一个类似的问题。 尝试了我在网上看到的所有建议但没有成功。 直到我必须在发出post请求之前手动将Content-Type设置为application/x-www-form-urlencoded

你可以这样做:

 $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded"; 

我已经解决了这个问题:你正在制作一个跨域请求,从http到https。

这在规范中没有描述,但您可以在chrome中测试它,这会忽略该问题。

您必须坚持所有http或所有https。

祝好运。

除非您要发布的服务器接受AngularJS OPTIONS请求,否则您无法从服务器执行POST 。 您需要配置和设置服务器以接受此请求; 根据语言的不同,可以通过CORS server setup +LANGUAGE找到许多指南。

一些API使用的一个鲜为人知的技巧是使用JSONP。 AngularJS JSONP调用可以像这样完成:

 $http.jsonp('https://api.endpoint.com/post-json?id=ID-KEY&callback=JSON_CALLBACK') .success(function(data, status) {... }) 

这也需要服务器的一些工作,但如果你想要消费第三方,可能是你的选择。

更新:您可能缺少Access-Control-Allow-Methods:POST, GET, OPTIONS, PUT, DELETE在您的服务器响应标头中。