好的,我有一个AngularJS应用程序,Laravel 4作为后端。我想要开发的方式是在一个单独的服务器上运行Laravel(通过工匠服务),并在一个单独的角度上运行,并且yeoman为我组装所有那些grunt服务器。
然而,在尝试通过Angulars $ http从Laravel 4后端(用作REST api)获取数据后,我得到的是0错误消息。这是我的代码:
Angular 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";
});
现在所有这一切都有效,如果我不使用grunt服务和工匠,并把它全部放在同一个文件夹(角落里面laravels公共文件夹等),我打算去生产,但因为我想要在此之前用grunt缩小并做其他事情我想保持这些元素分开。
通过一些谷歌搜索,我认为问题在于CORS(交叉原始请求),但我无法理解如何在此设置中解决问题。
答案 0 :(得分:0)
尝试在工厂声明中添加$ http服务:
app.factory('RestService', function($http){
答案 1 :(得分:0)
试试这个:
$scope.url = 'http://localhost:8000/api/v1/tags';
$scope.content = [];
$scope.fetchContent = function()
{
$http.get($scope.url).then(function(result)
{
$scope.content = result.data;
});
}
$scope.fetchContent();
还尝试返回一组数据。 +也返回响应代码。例如:
return Response::json(array(
'message' => 'This is a temporary message to test routing'), 200);