我怎样才能发布数据,表单数据,而不是一个请求负载? [英] How can I post data as form data instead of a request payload?

查看:118
本文介绍了我怎样才能发布数据,表单数据,而不是一个请求负载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的code时,AngularJS $ HTTP 方法调用的URL,并提交XSRF对象作为请求负载(如Chrome浏览器描述调试器的网络选项卡)。 jQuery的 $。阿贾克斯方法做同样的电话,但提交XSRF为表单数据。

In the code below, the AngularJS $http method calls the URL, and submits the xsrf object as a "Request Payload" (as described in the Chrome debugger network tab). The jQuery $.ajax method does the same call, but submits xsrf as "Form Data".

如何让AngularJS提交请求负载的XSRF为形式的数据呢?

How can I make AngularJS submit xsrf as form data instead of a request payload?

var url = 'http://somewhere.com/';
var xsrf = {fkey: 'xsrf key'};

$http({
    method: 'POST',
    url: url,
    data: xsrf
}).success(function () {});

$.ajax({
    type: 'POST',
    url: url,
    data: xsrf,
    dataType: 'json',
    success: function() {}
});

推荐答案

下面的行需要被添加到传递所述$的http对象

The following line needs to be added to the $http object that is passed:

headers: {'Content-Type': 'application/x-www-form-urlencoded'}

和传递应转换为URL-CN codeD字符串数据:

And the data passed should be converted to a URL-encoded string:

> $.param({fkey: "key"})
'fkey=key'

所以,你有这样的:

So you have something like:

$http({
    method: 'POST',
    url: url,
    data: $.param({fkey: "key"}),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})

从:<一href="https://groups.google.com/forum/#!msg/angular/5nAedJ1LyO0/4Vj_72EZcDsJ">https://groups.google.com/forum/#!msg/angular/5nAedJ1LyO0/4Vj_72EZcDsJ

这篇关于我怎样才能发布数据,表单数据,而不是一个请求负载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆