【问题标题】:Error Access-Control-Allow-Origin With Angular.js and API with Laravel5.1使用 Angular.js 和使用 Laravel5.1 的 API 出现错误 Access-Control-Allow-Origin
【发布时间】:2015-07-22 21:15:48
【问题描述】:

我正在使用 Laravel 5.1 和 Angular.js 开发应用程序,当我尝试连接时出现下一个错误:

XMLHttpRequest cannot load http://localhost:8000/university. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

我有下一个 angular 代码:

angular.module('clientApp')

.controller('UniversityCtrl', function ($scope, $resource) {
  Universities = $resource("http://localhost:8000/university/:id",{id: "@id"});

  $scope.Universities = Universities.query();
});

我的看法:

<table>
 <thead>
   <tr>
     <th>University</th>
     <th>Address</th>
     <th>Phone</th>
   </tr>
</thead>

 <tbody>
   <tr ng-repeat="Universities in University">
      <td>{{University.name}}</td>
   </tr>
 </tbody>
</table>

还有我的 Laravel 代码:

    public function index()
{
    $universities = university::all();
    return response()->json(
        $universities->toArray()
    );
}

谢谢:D

【问题讨论】:

  • 对前端和后端使用相同的端口或实施 CORS。不同的端口=跨域。很容易用谷歌搜索该错误以获取完整的详细信息
  • 如何实现CORS?
  • 通过研究......每天都会在这里提出问题,并且在谷歌搜索中有很多资源可以解释它

标签: javascript php angularjs laravel-5.1


【解决方案1】:

由 barryvdh 单独安装此 package

【讨论】:

    猜你喜欢
    • 2013-10-28
    • 1970-01-01
    • 2014-01-12
    • 2017-12-29
    • 2017-06-15
    • 2023-03-15
    • 2015-07-14
    • 2019-09-20
    • 2017-03-03
    相关资源
    最近更新 更多