我试图向http://battle.platform45.com/register
发出一个简单的POST请求--服务器是用来用JSON响应的。因此,从命令行:
$ curl --data '{"name": "Connor", "email": "connorleech@gmail.com"}' http://battle.platform45.com/register
成功返回
{"id":"3118","x":1,"y":9}%
我试着用角度来做同样的事情:
app.controller('BattleshipCtrl', function ($scope, $http) {
$scope.game = {}
$scope.newGame = function(name, email){
$http.post("http://battle.platform45.com/register", {
name: name,
email: email
}).success(function(data, status, headers, config){
console.log('Success')
})
}
});
有一个简单的观点:
<input type='text' ng-model='game.name'>
<input type='text' ng-model='game.email'>
<div class='btn btn-success' ng-click='newGame(game.name, game.email)'>New game</div>
当我试图提出请求时,我会得到一个错误:
OPTIONS http://battle.platform45.com/register 404 (Not Found) angular.js:7962
XMLHttpRequest cannot load http://battle.platform45.com/register. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://0.0.0.0:9000' is therefore not allowed access.
如何通过后要求的卷曲,但不是角?我怎样才能用角度成功地提出请求呢?
发布于 2014-05-07 23:41:49
我相信battle.platform45不允许浏览器直接调用他们的服务器。默认情况下,来自不同域的网站无法从其他网站访问API资源。
按照platform45的说法,您必须使用Rails来创建一个游戏。因此,我建议创建您自己的服务器,该服务器将从他们的API服务器调用。然后使用您的AngularJS代码访问您的服务器。
我希望这张图能更好地说明这一点:
Battle.platform45服务器-(被调用)->您的服务器
发布于 2014-05-07 23:26:14
尝试这样做:
var config = {headers: {
'Access-Control-Allow-Origin': '*'
}
};
app.controller('BattleshipCtrl', function ($scope, $http) {
$scope.game = {}
$scope.newGame = function(name, email){
$http.post("http://battle.platform45.com/register", config, {
name: name,
email: email
}).success(function(data, status, headers, config){
console.log('Success')
})
}
});
https://stackoverflow.com/questions/23535260
复制相似问题