与ngResource AngularJS:如何检查来自服务器的响应精确? [英] AngularJS with ngResource: How to check exact response from server?

查看:126
本文介绍了与ngResource AngularJS:如何检查来自服务器的响应精确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个角服务顶嘴在PHP做了一个简单的REST服务器。我可以看到,我能够获得一个记录,所有记录列表,并添加新的记录,但是,增加我有从服务器获取正确的反应,以便能够在它采取行动的问题的新纪录之后。

我知道这工作,因为越来越添加了新的纪录,不过,我想提出通知用户,如果不论什么原因,请求不起作用,等等...

这是该服务:

  angular.module('adminApp.services',['ngResource'])    .factory(设置,函数($资源){        返回$资源('休息/设置/:身份证',{ID:'@id'},{
            '查询':{方法:GET,则params:{},格式为:JSON,IsArray的:真正},
            保存:{方法:POST,则params:{},格式为:JSON,IsArray的:真正},
            '得到':{方法:GET,则params:{},格式为:JSON,IsArray的:假},
            更新:{方法:把',则params:{ID:'@id'},格式为:JSON,IsArray的:真正},
            删除:{方法:删除,则params:{ID:'@id'},格式为:JSON,IsArray的:假}
    });});

由于控制器的一部分,我有以下几点:

  $ scope.save =功能(){
    VAR的结果= Settings.save({} $ scope.settings);    的console.log(结果);    //这里我想将结果发送到对话框
    //确定羯羊或不请求成功
    // dialog.close(结果);
};

从HTTP请求的网络响应,因为通过它看到来自服务器返回的,但是,执行console.log(结果)返回真的字符数组的JavaScript控制台返回真 - 我已经猜到这是因为IsArray的的:

:在保存,因为PARAMS被发送到服务器阵列,虽然这是必要的true选项

  [$承诺:对象,$解决:虚假]
    0:T,
    1:R,
    2:U
    3:E,
    $承诺:对象,    //我试图传递的结果。$决心的对话框中,
    //但是如果它上面yousee解析为假往上顶第一
    $解决:真实,
    长度:4,
    __proto__:数组[0]

我知道HTTP响应的真值JSON,如果我能勾到,这将是容易的(我来自一个jQuery的背景,也许我做错了,我已成为一个点,删除的jQuery完全从这个项目中,以不让它阻碍我的学习)。

我想问题是,我怎么居然从服务器到JS变量,反应其实我可以一起工作?

编辑:更新时间:

我改变了我的服务:

  angular.module('adminApp.services',['ngResource'])
    .factory(设置,函数($ HTTP,$资源,$日志){
        返回$资源('休息/设置/:身份证',{ID:'@id'},{
            保存 : {
            方法:POST,
            PARAMS:{},
            格式为:JSON,
            IsArray的:真实,
            transformResponse:功能(数据,headersGetter){
                $ log.info(数据); //返回true
                返回{回应:数据};
            }]。CONCAT($ http.defaults.transformResponse)
        },
        更新:{方法:把',则params:{ID:'@id'},格式为:JSON,IsArray的:真正},
        删除:{方法:删除,则params:{ID:'@id'},格式为:JSON,IsArray的:假}
    });});

和调用:

  $ scope.save =功能(){
    $ scope.results = Settings.save({} $ scope.settings);
    的console.log($ scope.results); //仍返回$承诺响应
    //dialog.close(true);
};

但我仍然没有作为响应得到真正


解决方案

看到这个小提琴: http://jsfiddle.net/moderndegree/ Kn3Tc /

  angular.module('对myApp',['ngResource'])。
工厂('为myService',函数($ HTTP,$资源,$日志){
    返回$资源('/',{},{
        得到:{
            方法:GET,
            transformRequest:功能(数据,headersGetter){
                //你可以检查这里的原始请求
                $ log.info(数据);
                $ log.info(headersGetter());
            }]。CONCAT($ http.defaults.transformRequest)
            transformResponse:功能(数据,headersGetter){
                //您可以检查这里的原始响应
                $ log.info(数据);
                $ log.info(headersGetter());
                返回{田田:检查您的控制台};
            }]。CONCAT($ http.defaults.transformResponse)
        }
    });
})。
控制器('myController的'功能(为myService,$范围,$资源,$ HTTP,$日志){
    $ scope.results = myService.get();
});


  

要全局增加或覆盖默认的转换,修改
  $ httpProvider.defaults.transformRequest和
  $ httpProvider.defaults.transformResponse属性。


在这里阅读更多: http://docs.angularjs.org/api/ng $ HTTP

I have created an angular service to talk back to a simple REST server made in PHP. I can see that I am able to get a single record, list of all records, and add new records, however, after adding new records I am having an issue with getting the correct response from the server to be able to act upon it.

I know it's working because the new records are getting added, however, I wish to put notifications for users if for whatever reason the request does not work, and so forth...

This is the service:

angular.module('adminApp.services', ['ngResource'])

    .factory('Settings', function($resource) {

        return $resource('rest/setting/:id', {id: '@id'}, {
            'query' : { method: 'GET', params: {}, format: 'json', isArray: true },
            'save'  : { method: 'POST', params: {}, format: 'json', isArray: true },
            'get'   : { method: 'GET', params: {}, format: 'json', isArray: false },
            'update': { method: 'PUT', params: {id: '@id'}, format: 'json', isArray: true },
            'delete': { method: 'DELETE', params: {id: '@id'}, format: 'json', isArray: false }
    });

});

As part of the controller, I have the following:

$scope.save = function() {
    var result = Settings.save({}, $scope.settings);

    console.log(result);

    // Here I would like to send the result to the dialog
    // to determine wether or not the request was successful
    // dialog.close(result);
};

The Network response from the HTTP request, as seen via the javascript console returns 'true' which comes back from the server, however, the console.log(result) returns an array of the characters in 'true' -- I had guessed this is because of the isArray : true option in 'save' which is necessary because the params get sent to the server as an array though:

[$promise: Object, $resolved: false]
    0: "t",
    1: "r",
    2: "u",
    3: "e",
    $promise: Object,

    // I tried passing result.$resolved to the dialog,
    // but if yousee above it resolves to false up top first
    $resolved: true,
    length: 4,
    __proto__: Array[0]

I know the HTTP response is a json value of true, if I could hook into that it would be easy (I come from a jQuery background, maybe I'm doing it wrong, I have made it a point to remove jQuery completely from this project as to not allow it to inhibit my learning).

I guess the question is, how do I actually get that response from the server onto a JS variable I can actually work with?

Edit: Updated

I changed my service to:

angular.module('adminApp.services', ['ngResource'])
    .factory('Settings', function($http, $resource, $log) {
        return $resource('rest/setting/:id', {id: '@id'}, {
            save   : {
            method: 'POST',
            params: {},
            format: 'json',
            isArray: true,
            transformResponse: [function(data, headersGetter) {
                $log.info(data); // returns true
                return { response: data };
            }].concat($http.defaults.transformResponse)
        },
        update : { method: 'PUT', params: {id: '@id'}, format: 'json', isArray: true },
        delete : { method: 'DELETE', params: {id: '@id'}, format: 'json', isArray: false }
    });

});

And the call to:

$scope.save = function() {
    $scope.results = Settings.save({}, $scope.settings);
    console.log($scope.results); // Still returns the response with $promise
    //dialog.close(true);
};

But I'm still not getting true as the response

解决方案

See this fiddle: http://jsfiddle.net/moderndegree/Kn3Tc/

angular.module('myApp', ['ngResource']).
factory('myService', function($http, $resource, $log){
    return $resource('/', {}, {
        get: {
            method: 'GET',
            transformRequest: [function(data, headersGetter){
                // you can examine the raw request in here
                $log.info(data);
                $log.info(headersGetter());
            }].concat($http.defaults.transformRequest),
            transformResponse: [function (data, headersGetter) {
                // you can examine the raw response in here
                $log.info(data);
                $log.info(headersGetter());
                return {tada:"Check your console"};
            }].concat($http.defaults.transformResponse)
        }
    });
}).
controller('myController', function(myService, $scope, $resource, $http, $log) {
    $scope.results = myService.get();
});

To globally augment or override the default transforms, modify the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties.

read more here: http://docs.angularjs.org/api/ng.$http

这篇关于与ngResource AngularJS:如何检查来自服务器的响应精确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆