【发布时间】:2014-06-25 11:03:54
【问题描述】:
我正在尝试向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}%
我尝试在 Angular 中做同样的事情:
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.
POST 请求如何通过 curl 而不是 Angular 进行?我该怎么做才能成功地使用 Angular 发出请求?
【问题讨论】:
标签: javascript angularjs post http-post cors