Ng Http Request/response格式转换

简介:

angular作为Single Page Application推荐的交互方式当然是基于json的ajax调用。但今天要说的是当你不幸工作在一个遗留或者不可控制的服务上,而这服务是基于非json提交方式(或许是常规表单(form)提交,或者其他自定义数据格式),那么我们只能改变ng内部$http默认request/response格式转化方式。

所幸的是ng $http给我们提供了多种可用方式转化数据格式(下面demo将以form提交方式为例):

***对于部分单独的http request设置:

对于http ajax方式最后一个参数都是关于http的配置信息,其中包括一项transformRequest,我们可以利用transformRequest在ajax发送数据之前改变数据的格式,例如下边的demo:

复制代码
$http.post("/url", {
      id: 1,
      name: "greengerong"
    }, {
      transformRequest: function(request) {
        return $.param(request);
    }
});
复制代码

这里利用jQuery的$.param进行表单提交方式的格式转化,所以我们能够看见的request body 为:

id=1&name=greengerong

online demo;

***对于整个app的http request设置:

如果我们需要对整个http的数据转化格式进行设置,那么可以选用在config阶段对$httpProvider默认行为进行设置:

复制代码
angular.module("app", [])
.config(["$httpProvider", function($httpProvider) {
      $httpProvider.defaults.transformRequest = [
        function(request) {
          return $.param(request);
        }
      ];
    }
]);
复制代码

这样我们就可以轻易的转化为form提交方式。

同样$http也为我们提供了transformResponse方式,我们也可以创建自己的response转化,比如json之前加入自定义前缀防止json array攻击等等。


本文转自破狼博客园博客,原文链接:http://www.cnblogs.com/whitewolf/p/3952820.html,如需转载请自行联系原作者

目录
相关文章
|
移动开发 负载均衡 安全
Web Security 之 HTTP request smuggling(上)
Web Security 之 HTTP request smuggling
356 0
npm 安装出错 npm ERR! request to https://registry.npmjs.org/express failed, reason: unable to verify th
npm 安装出错 npm ERR! request to https://registry.npmjs.org/express failed, reason: unable to verify th
960 0
verbose stack FetchError: request to https://registry.npm.taobao.org/md-editor-v3 failed, reason: ce
这篇文章描述了在安装npm包`md-editor-v3`时遇到的淘宝镜像证书过期问题,并提供了解决方案,即通过切换npm镜像源到`https://registry.npmmirror.com/`来解决安装失败的问题。
verbose stack FetchError: request to https://registry.npm.taobao.org/md-editor-v3 failed, reason: ce
|
4月前
|
安全 应用服务中间件 网络安全
修复HTTPS升级后出现 Mixed Content: The page at 'https://xxx' was loaded over HTTPS, but requested an insecure frame 'http://xxx'. This request has been blocked; the content must be served over HTTPS. 的问题
修复HTTPS升级后出现 Mixed Content: The page at 'https://xxx' was loaded over HTTPS, but requested an insecure frame 'http://xxx'. This request has been blocked; the content must be served over HTTPS. 的问题
|
5月前
|
测试技术 API
8-20|https://gitlab.xx.com/api/v4/projects/4/trigger/pipeline Request failed 状态码400
8-20|https://gitlab.xx.com/api/v4/projects/4/trigger/pipeline Request failed 状态码400
|
6月前
|
JavaScript
request to https://registry.npm.taobao.org/cnpm failed, reason: certificate has expired
request to https://registry.npm.taobao.org/cnpm failed, reason: certificate has expired
184 2
|
JSON 小程序 前端开发
小程序踩坑-http://xxx.com 不在以下 request 合法域名列表中
小程序踩坑-http://xxx.com 不在以下 request 合法域名列表中
226 0
|
XML 开发框架 Java
Java Web 项目入门指南(http、Servlet、Request、Response、ServletContext、会话技术[cookie、session]、Filter、Listener)
Java Web 项目入门指南(http、Servlet、Request、Response、ServletContext、会话技术[cookie、session]、Filter、Listener)
106 0
|
9月前
|
API 数据格式
8-20|https://gitlab.xx.com/api/v4/projects/4/trigger/pipeline Request failed状态码400
根据具体情况,逐步检查这些因素,找到引发400状态码的原因,并进行相应的修复。
140 0

热门文章

最新文章