首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用AngularJs理解Laravel 5 Dingo/Api的API响应;正确的JSON格式是什么?

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

Stack Overflow用户
提问于 2015-10-27 08:12:13
回答 1查看 1.3K关注 0票数 1

我正在为Laravel 5 Web应用程序构建一个API,使用AngularJs应用程序作为API使用者。

除了从AngularJS调用时从API返回的响应之外,一切都工作得很好。

这里是我在AngularJs应用程序中使用的,它也使用Satellizer

代码语言:javascript
运行
复制
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控制器

代码语言:javascript
运行
复制
<?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获取概要文件对象,我必须这样做:

代码语言:javascript
运行
复制
return $http.get('/api/profile').then(function(data){
   return data.data.profile;
}); 

如何使API只返回配置文件对象,而不发送configheader和其他对象?真的有必要吗?我想简单地这样做:

代码语言:javascript
运行
复制
return $http.get('/api/profile').then(function(data){
   return data; //which contains only profile object
}); 

编辑:我想我的问题是:这是来自Dingo Api的正确的JSON响应格式吗?

代码语言:javascript
运行
复制
{
  "config",
  "data": {
    "id": 1001,
    "name": "Wing"
  },
  "headers",
  "status",
  "statusText"
}
EN

回答 1

Stack Overflow用户

发布于 2015-10-27 08:17:36

您是否试图从控制器返回一个响应,而不是一个雄辩的对象:

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

您可以指定您到底需要什么(比如配置文件)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33362739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档