javascript – AngularJS:装饰$http

我有一个简单的控制器,如:
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.中的更多内容

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...