【问题标题】:AngularJS: reading both data and headers from a HttpResponseAngularJS:从 HttpResponse 读取数据和标头
【发布时间】:2016-07-31 13:24:12
【问题描述】:

我有一个 REST API(使用 Spring Boot 开发),我在前端使用 AngularJS 1.5,登录服务除了数据之外还发送一个标头,我如何在 angularjs 端读取数据和标头

var LocalBusinessUserResource = $resource(apiService + '/localbusinessusers/login');
var deferred = $q.defer();
            LocalBusinessUserResource.get(credantials, function (result) {
                console.log('result: ' + JSON.stringify(result));
                deferred.resolve(result);
            }, function () {
                console.error('check your server connection');
            });
            return deferred.promise;

【问题讨论】:

    标签: javascript angularjs spring http-headers httpresponse


    【解决方案1】:

    来自$http docs

    响应对象具有以下属性:

    data – {string|Object} – The response body transformed with the transform functions.
    status – {number} – HTTP status code of the response.
    headers – {function([headerName])} – Header getter function.
    config – {Object} – The configuration object that was used to generate the request.
    statusText – {string} – HTTP status text of the response.
    

    您可以访问您从中获取响应数据的同一对象上的响应标头。

    【讨论】:

    • 我没有使用 $http 并且我所有的电话我都使用了 $resource
    • 啊,我的错。在这种情况下,您应该能够通过向回调添加第二个参数来访问响应标头,如下所示:LocalBusinessUserResource.get(credantials, function (result, responseHeaders) {...;
    猜你喜欢
    • 1970-01-01
    • 2011-09-19
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多