在AngularJs中使用$ http.get时出现跨源阻止请求(CORS)错误。 (Homestead Lararevel为后端)

时间:2015-05-19 18:27:52

标签: angularjs api rest xmlhttprequest homestead

我在AngularJs中使用$ http.get时遇到跨源阻止请求(CORS)...错误。我已经安装了Laravel和Homestead。 我的AngularJs代码(frontend.app:8000)看起来像这样:

<div ng-app="myApp" ng-controller="customersCtrl">

<ul>
  <li ng-repeat="x in names">
    {{ x.Name + ', ' + x.Country }}
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://demo.app:8000")
    .success(function(response) {$scope.names = response.records;});
});
</script> 

demo.app:8000返回JSON,如下所示:

{
"records": [

  {
    "Name" : "The Big Cheese",
    "City" : "Portland",
    "Country" : "USA"
  },
  {
    "Name" : "Vaffeljernet",
    "City" : "Århus",
    "Country" : "Denmark"
  },
  {
    "Name" : "Wolski Zajazd",
    "City" : "Warszawa",
    "Country" : "Poland"
  }
]
}

我的Homestead.yaml看起来像这样:

---
ip: "192.168.10.10"
memory: 2048
cpus: 1

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Documents/core
      to: /home/vagrant/Documents/core

sites:
    - map: demo.app
      to: /home/vagrant/Documents/codes/demo/public
    - map: frontend.app
      to: /home/vagrant/Documents/codes/frontend

variables:
    - key: APP_ENV
      value: local

当进入前端(角度代码)http://frontend.app:8000时,我得到了交叉原点错误。任何帮助表示赞赏。

0 个答案:

没有答案