douchuifk90315 2015-10-27 08:12
浏览 86

了解Laravel 5 Dingo / Api使用AngularJs的API响应; 什么是正确的JSON格式

I am building an API for a Laravel 5 Web application using AngularJs App as API consumer.

Everything works perfectly except for the response returned from the API when a call is made from AngularJS.

Here is what I have in AngularJs App which also uses 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
            });
        };

    }]);

In my Laravel Controller

<?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);
    }
}

Response from Laravel enter image description here

The challenge I have is in the response above.

As you can see in the resolve method of Angular Ui Router, To get the profile object from the returned JSON, I had to do this:

return $http.get('/api/profile').then(function(data){
   return data.data.profile;
}); 

How do I make the API to return only profile object without config, header and other objects sent along? Are they really necessary? I would like to simply do this:

return $http.get('/api/profile').then(function(data){
   return data; //which contains only profile object
}); 

Edited: I think my question is; Is this the right JSON response Format from Dingo Api?

{
  "config",
  "data": {
    "id": 1001,
    "name": "Wing"
  },
  "headers",
  "status",
  "statusText"
}
  • 写回答

1条回答 默认 最新

  • dongyizhuang0134 2015-10-27 08:17
    关注

    Have you tried to return a response from the controller, not an Eloquent object:

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

    You can specify what exactly you need there (like profile).

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值