【问题标题】:Understanding API response from Laravel 5 Dingo/Api Using AngularJs; What's the correct JSON Format使用 AngularJs 理解来自 Laravel 5 Dingo/Api 的 API 响应;什么是正确的 JSON 格式
【发布时间】:2016-01-26 13:19:37
【问题描述】:

我正在使用 AngularJs 应用程序作为 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);
    }
}

来自 Laravel 的回应

我面临的挑战是在上面的回复中。

正如您在 Angular 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"
}

【问题讨论】:

    标签: php angularjs api laravel dingo-api


    【解决方案1】:

    您是否尝试从控制器返回响应,而不是 Eloquent 对象:

    http://laravel.com/docs/master/responses.

    您可以在那里指定您确切需要的内容(例如配置文件)。

    【讨论】:

    • 感谢您的回答。也许小例子会有所帮助
    • 嗯,这取决于你在Profile 对象中到底有什么,但它可能类似于` public function getIndex(){ $user = $this->auth->user( ); $u = Profile::find($user->id); return response()->json(['profile' => $u->profile]); }`
    猜你喜欢
    • 2021-03-16
    • 2014-08-24
    • 1970-01-01
    • 2016-07-22
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多