我有一个使用AngularJS构建的应用程序和一个以JSON形式交付所有请求的服务器端后端.每个请求都包装在一个JSON容器中,该容器包含一个数据变量,该变量包含特定于该请求的数据.其他数据,用于保持应用程序内的状态和控制,判断错误和成功消息,以及判断会话标志.所有这些其他变量都随每个请求一起提供,并且在判断数据变量之前首先进行判断.

现在我有了一种方法,可以先判断JSON响应的内容,然后判断数据本身.

$http.get('something.json').success(function(response) {
   var data = examineJSONResponse(response);
   //do the data stuff
});

This works and the examineJSONResponse takes a look at the code and if there is something wrong then it throws an exception and reloads the page using window.location.href.

Is there any way that I can automate this within AngularJS so that each time a $http call is made then it checks this and ONLY returns the data variable contents as the JSON response?

推荐答案

You can intercept responses by adding an interceptor to $httpProvider.interceptors with Angular 1.1.4+ (see documentation here search for interceptors).

For a specific content type like json you can potentially reject changes or throw an exception even if the call was a success. You can modify the response.data that will get passed to your controller code as well here:

myModule.factory('myHttpInterceptor', function ($q) {
    return {
        response: function (response) {
            // do something on success
            if(response.headers()['content-type'] === "application/json; charset=utf-8"){
                // Validate response, if not ok reject
                var data = examineJSONResponse(response); // assumes this function is available

                if(!data)
                    return $q.reject(response);
            }
            return response;
        },
        responseError: function (response) {
            // do something on error
            return $q.reject(response);
        }
    };
});
myModule.config(function ($httpProvider) {
    $httpProvider.interceptors.push('myHttpInterceptor');
});

NOTE:以下是1.1.4之前版本的原始答案(responseInterceptors个版本被Angular 1.1.4弃用):

Maybe there's a better way but I think you can do something similar to this post with the http response interceptor (described here) (for a specific content type like json) where you potentially reject changes or throw an exception even though the call was a success. You can modify the response.data that will get passed to your controller code as well here.

myModule.factory('myHttpInterceptor', function ($q) {
    return function (promise) {
        return promise.then(function (response) {
            // do something on success
            if(response.headers()['content-type'] === "application/json; charset=utf-8"){
                // Validate response if not ok reject
                var data = examineJSONResponse(response); // assumes this function is available

                if(!data)
                    return $q.reject(response);
            }
            return response;
        }, function (response) {
            // do something on error
            return $q.reject(response);
        });
    };
});
myModule.config(function ($httpProvider) {
    $httpProvider.responseInterceptors.push('myHttpInterceptor');
});

Json相关问答推荐

如何在JMESPath中区分空和假?

在ConvertFrom-Json之后需要从PowerShell对象中获取数据

如何在VB6中将字符串转换或解码为可读格式?

使用 Powershell,如何将 Azure AD 组成员转换为 Json 对象(文件),然后可以更新?

展平多个数组以保持顺序

使用 Power BI 中的 Deneb 视觉效果绘制面积图中的 X 轴日期

使用 BASH 和 JQ 我想将 json 文件与 bash 数组进行比较

boost::json::value 的大括号初始化将其从对象转换为数组

Microsoft GRAPH 查询使用端点 /deviceManagement/deviceHealthScripts 列出了一种不熟悉的检测脚本内容格式

无法向 Json 数组添加新元素

如何解决名为 null 的map值

如何让 JSON.NET 忽略对象关系?

Json.NET SerializeObject 转义值以防止 XSS

Java JSON 序列化 - 最佳实践

什么是类型和类型令牌?

如何使用 SwiftyJSON 将字符串转换为 JSON

如何将有向无环图 (DAG) 存储为 JSON?

Django:TypeError:[] 不是 JSON 可序列化的为什么?

如何将 MongoDB 查询转换为 JSON?

设置对象不是 JSON 可序列化的