我实际上是在网上搜索,寻找使用 AngularJS 将数据发布到服务器的正确方法。
当我使用 Angular JS 执行 $http POST 请求时,它会将我的数据发布到服务器,但失败并显示错误消息“ http://localhost:8000
Access-Control-Allow-Origin 不允许来源”,如果我尝试使用“文件: ///",结果还是一样。
我尝试了互联网上提到的所有补救措施,包括
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