javascript - AngularJS:装饰 $http

标签 javascript ajax angularjs

我有一个简单的 Controller ,例如:

function MyController($scope, $http) {
    ...
    $http.post(url).success(function(data) {
            console.log(data)
         });
}
MyController.$inject = ['$scope', '$http'];

一切都按预期工作,但我遇到了问题。出于安全原因,返回的 JSON 用/** */注释。使用 jQuery,我扩展了 $.ajax 对象以删除此注释,然后解析结果。我想用 AngularJS 实现同样的效果,并以某种方式告诉 $http 也从每个响应中删除评论。我想为我的整个应用程序执行此操作,并避免键入始终相同的内容。

有什么想法可以做到这一点吗?

最佳答案

您将要转换所有的 $http 响应。我以前没有这样做过,但相关文档在下面。

Transforming Requests and Responses

Both requests and responses can be transformed using transform functions. By default, Angular applies these transformations:

Request transformations:

  • if the data property of the request config object contains an object, serialize it into JSON format.

Response transformations:

  • if XSRF prefix is detected, strip it (see Security Considerations section below)
  • if json response is detected, deserialize it using a JSON parser

To override these transformation locally, specify transform functions as transformRequest and/or transformResponse properties of the config object. To globally override the default transforms, override the $httpProvider.defaults.transformRequest and $httpProvider.defaults.transformResponse properties of the $httpProvider.

$http service documentation. 中阅读更多内容

关于javascript - AngularJS:装饰 $http,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11740289/

相关文章:

Javascript计算公式

javascript - 通过 $.each 循环中的键读取 JSON 数据值

javascript - 向 knockout 映射添加额外的对象属性

jQuery POST AJAX => 无响应 header

javascript - Angular ng-repeat 过滤器在数据检索之前运行

javascript - 合并多个字段输入的搜索结果

javascript - 数组新方法

jquery - 使用 jQuery 更新动态/ajax 表行的替代颜色

javascript - AngularJS - 禁用 ng 的错误?不更新新值

javascript - 如何使用 D3 从 Bower 安装 D3 到 Angular 项目