与angularjs 1.2.6跨起源请求错误起源、错误、angularjs

2023-09-13 04:04:53 作者:こ撕掉你那虚伪的伪装

我试图做一个简单的POST请求 http://battle.platform45.com/register 的服务器所使用JSON回应。因此,从命令行:

I am trying to make a simple POST request to http://battle.platform45.com/register The server is setup to respond with JSON. So from the command line:

$卷曲--data{名字:康纳,电子邮件:connorleech@gmail.com}'http://battle.platform45.com/register

成功返回

{ID:3118,X:1,Y:9}%

我尝试做同样的事情在角:

I try to do the same thing in 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')
        })
    }
});

用一个简单的观点:

with a simple view:

<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>

当我试着让我的请求得到一个错误:

When I try to make the request I get an error:

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请求,卷曲,但没有棱角办理什么手续?我能做些什么,也成功地与角度的要求?

How does the POST request go through with curl but not angular? What can I do to successfully make the request with angular?

推荐答案

我相信battle.platform45是的不的允许网页浏览器做他们的服务器直接调用。默认情况下,来自不同领域的网站不能从其他网站访问API的资源。

I believe battle.platform45 is not allowing web browsers to do direct calls to their servers. By default, websites from different domains cannot access API resources from other websites.

由于根据platform45,必须使用Rails创建一个游戏。所以我建议创建自己的服务器,将他们的API服务器调用。然后用你的AngularJS code来访问你的服务器。

As according to platform45, you must use Rails to create a game. So I suggest creating your own server that will call from their API server. Then use your AngularJS code to access your server.

我希望这个图可以更好地说明它:

I hope this diagram can illustrate it better:

Battle.platform45服务器 - (被称为) - >您的服务器 - (被称为) - >您的HTML / JS角code

Battle.platform45 Server -- (is called by) --> Your Server -- (is called by) --> Your HTML/Angular JS Code