我正在为Laravel 5 Web应用程序构建一个API,使用AngularJs应用程序作为API使用者。
除了从AngularJS调用时从API返回的响应之外,一切都工作得很好。
这里是我在AngularJs应用程序中使用的,它也使用Satellizer
var app = angular
.module('app', [
'ngResource',
'ui.bootstrap',
'dialogs.main',
'ui.router',
'satellizer',
'ui.router.stateHelper',
'templates'
]);
app.config(['$httpProvider', '$locationProvider', '$stateProvider', '$urlRouterProvider', 'modalStateProvider', '$authProvider',
function($httpProvider, $locationProvider, $stateProvider, $urlRouterProvider, modalStateProvider, $authProvider)
{
var modalInstance,
modalExit = function() {
if (modalInstance) {
//alert('modalInstance exit');
modalInstance.close();
}
};
// Satellizer configuration that specifies which API
// route the JWT should be retrieved from
$authProvider.loginUrl = '/api/authenticate';
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
$stateProvider
.state('profile',{
url: '/profile',
views: {
'contentFullRow': {
templateUrl: 'ng/templates/profile/partials/profile-heading-one.html',
controller: function($scope, profile){
$scope.profile = profile;
}
},
'contentLeft': {
templateUrl: 'ng/templates/profile/partials/profile-body-one.html',
controller: function($scope, profile){
$scope.profile = profile;
}
},
'sidebarRight': {
templateUrl: 'ng/templates/profile/partials/todo-list-one.html',
controller: function($scope, profile){
$scope.profile = profile;
}
}
},
resolve: {
profile: function($http){
return $http.get('/api/profile').then(function(data){
//This is the issue, I am doing this because of the response returned
return data.data.profile;
});
}
}
});
if(window.history && window.history.pushState){
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
};
}]);我的Laravel控制器
<?php namespace App\Http\Controllers\Profile;
use App\Http\Controllers\Controller;
use App\Models\Profile;
class ProfileController extends Controller
{
public function __construct()
{
$this->middleware('api.auth');
}
public function getIndex(){
$user = $this->auth->user();
return Profile::find($user->id);
}
}来自Laravel的响应

我面临的挑战是在上面的回应中.
正如您在角Ui路由器的resolve方法中看到的那样,要从返回的JSON获取概要文件对象,我必须这样做:
return $http.get('/api/profile').then(function(data){
return data.data.profile;
}); 如何使API只返回配置文件对象,而不发送config、header和其他对象?真的有必要吗?我想简单地这样做:
return $http.get('/api/profile').then(function(data){
return data; //which contains only profile object
}); 编辑:我想我的问题是:这是来自Dingo Api的正确的JSON响应格式吗?
{
"config",
"data": {
"id": 1001,
"name": "Wing"
},
"headers",
"status",
"statusText"
}发布于 2015-10-27 08:17:36
您是否试图从控制器返回一个响应,而不是一个雄辩的对象:
http://laravel.com/docs/master/responses。
您可以指定您到底需要什么(比如配置文件)。
https://stackoverflow.com/questions/33362739
复制相似问题