我正在使用 AngularJs App 作为 API 使用者为 Laravel 5 Web 应用程序构建 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);
}
}
我面临的挑战是在上面的回应中。
正如您在resolve
Angular Ui Router 的方法中看到的那样,要从返回的 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"
}