好吧,我有一个以Laravel 4作为后端的AngularJS应用程序。我想要开发的方式是通过在单独的服务器上运行Laravel(通过工匠服务),以及与yeoman为我组装所有grunt服务器的单独角色。
但是,当试图通过Angulars $ http从Laravel 4后端(作为REST API)获取数据后,我得到的是0错误消息。这里是我的代码:
角REST服务:
app.factory('RestService', function($http){
var restUrl = 'http://localhost:8000/api/v1/tags';//<-- rest api served with php artisan
return {
getResource: function(){
return $http({method: 'get', url: restUrl})
}
}
});
角控制器:
app.controller('TagCtrl', function($scope, RestService){
$scope.getTagList = RestService.getResource()
.success(function(){
console.log('Success!');
})
.error(function(data, status, headers, config){
console.log(data);//<--returns nothing
console.log(status);//<--returns 0
console.log(headers);//<-- returns function
console.log(config);//<-- returns object
});
});
这里是我的Laravel 4路线:
Route::get('tags', function(){
return "This is a temporary message to test routing";
});
现在,当这一切工作,如果我不使用咕噜服务和工匠,并把它放在同一个文件夹(在laravels公共文件夹等角度)当我转向生产时我打算做这些工作,但是因为我想在这之前先咕噜咕噜地做其他事情,所以现在我想让这些元素保持独立。
随着一些谷歌搜索我认为问题是与CORS(交叉来源请求),但我不明白如何解决在这种设置问题。
使用开发人员工具在Chrome中运行它。如果你的脚本client/api在localhost上,那么CORS就没有问题。 – Whisher