【发布时间】: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