mji*_*son 517 ajax post angularjs angular-http
在下面的代码中,AngularJS $http方法调用URL,并将xsrf对象作为"请求有效负载"提交(如Chrome调试器网络选项卡中所述).jQuery $.ajax方法执行相同的调用,但将xsrf提交为"Form Data".
如何让AngularJS将xsrf作为表单数据而不是请求有效负载提交?
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() {}
});
Run Code Online (Sandbox Code Playgroud)
mji*_*son 608
需要将以下行添加到传递的$ http对象中:
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
Run Code Online (Sandbox Code Playgroud)
并且传递的数据应转换为URL编码的字符串:
> $.param({fkey: "key"})
'fkey=key'
Run Code Online (Sandbox Code Playgroud)
所以你有类似的东西:
$http({
method: 'POST',
url: url,
data: $.param({fkey: "key"}),
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
})
Run Code Online (Sandbox Code Playgroud)
来自:https://groups.google.com/forum/#!msg/angular/5nAedJ1LyO0/4Vj_72EZcDsJ
要使用AngularJS V1.4添加的新服务,请参阅
Ant*_*ony 193
如果你不想在解决方案中使用jQuery,你可以试试这个.从这里获得解决方案/sf/answers/120042961/
$http({
method: 'POST',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: xsrf
}).success(function () {});
Run Code Online (Sandbox Code Playgroud)
Eze*_*tor 91
围绕这个问题的持续困惑激发了我写一篇关于它的博客文章.我在这篇文章中提出的解决方案优于您当前的最高评级解决方案,因为它不会限制您为$ http服务调用参数化数据对象; 即使用我的解决方案,您可以继续将实际数据对象传递给$ http.post()等,并仍然可以实现所需的结果.
此外,最受欢迎的答案依赖于在$ .param()函数的页面中包含完整的jQuery,而我的解决方案是jQuery不可知,纯粹的AngularJS就绪.
http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/
希望这可以帮助.
kza*_*zar 82
我拿了一些其他的答案并做了一些更清洁的东西,把这个.config()调用放在app.js中angular.module的末尾:
.config(['$httpProvider', function ($httpProvider) {
// Intercept POST requests, convert to standard form encoding
$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
$httpProvider.defaults.transformRequest.unshift(function (data, headersGetter) {
var key, result = [];
if (typeof data === "string")
return data;
for (key in data) {
if (data.hasOwnProperty(key))
result.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
}
return result.join("&");
});
}]);
Run Code Online (Sandbox Code Playgroud)
Mit*_*tja 57
从AngularJS v1.4.0开始,有一个内置$httpParamSerializer服务,可以根据文档页面上列出的规则将任何对象转换为HTTP请求的一部分.
它可以像这样使用:
$http.post('http://example.com', $httpParamSerializer(formDataObj)).
success(function(data){/* response status 200-299 */}).
error(function(data){/* response status 400-999 */});
Run Code Online (Sandbox Code Playgroud)
请记住,对于正确的表单帖子,Content-Type必须更改标题.要为所有POST请求全局执行此操作,可以使用此代码(取自Albireo的半答案):
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
Run Code Online (Sandbox Code Playgroud)
要仅对当前帖子执行此操作,headers需要修改request-object 的属性:
var req = {
method: 'POST',
url: 'http://example.com',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: $httpParamSerializer(formDataObj)
};
$http(req);
Run Code Online (Sandbox Code Playgroud)
Alb*_*reo 24
您可以全局定义行为:
$http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
Run Code Online (Sandbox Code Playgroud)
所以你不必每次都重新定义它:
$http.post("/handle/post", {
foo: "FOO",
bar: "BAR"
}).success(function (data, status, headers, config) {
// TODO
}).error(function (data, status, headers, config) {
// TODO
});
Run Code Online (Sandbox Code Playgroud)
Jam*_*ell 20
作为一种解决方法,您可以简单地使接收POST的代码响应application/json数据.对于PHP,我添加了下面的代码,允许我以form-encoded或JSON对它进行POST.
//handles JSON posted arguments and stuffs them into $_POST
//angular's $http makes JSON posts (not normal "form encoded")
$content_type_args = explode(';', $_SERVER['CONTENT_TYPE']); //parse content_type string
if ($content_type_args[0] == 'application/json')
$_POST = json_decode(file_get_contents('php://input'),true);
//now continue to reference $_POST vars as usual
Run Code Online (Sandbox Code Playgroud)
Ser*_*gan 16
这些答案看起来像疯狂的矫枉过正,有时,简单就是更好:
$http.post(loginUrl, "userName=" + encodeURIComponent(email) +
"&password=" + encodeURIComponent(password) +
"&grant_type=password"
).success(function (data) {
//...
Run Code Online (Sandbox Code Playgroud)
小智 9
您可以尝试以下解决方案
$http({
method: 'POST',
url: url-post,
data: data-post-object-json,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for (var key in obj) {
if (obj[key] instanceof Array) {
for(var idx in obj[key]){
var subObj = obj[key][idx];
for(var subKey in subObj){
str.push(encodeURIComponent(key) + "[" + idx + "][" + encodeURIComponent(subKey) + "]=" + encodeURIComponent(subObj[subKey]));
}
}
}
else {
str.push(encodeURIComponent(key) + "=" + encodeURIComponent(obj[key]));
}
}
return str.join("&");
}
}).success(function(response) {
/* Do something */
});
Run Code Online (Sandbox Code Playgroud)
为帖子创建适配器服务:
services.service('Http', function ($http) {
var self = this
this.post = function (url, data) {
return $http({
method: 'POST',
url: url,
data: $.param(data),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
}
})
Run Code Online (Sandbox Code Playgroud)
在控制器或其他任何地方使用它:
ctrls.controller('PersonCtrl', function (Http /* our service */) {
var self = this
self.user = {name: "Ozgur", eMail: null}
self.register = function () {
Http.post('/user/register', self.user).then(function (r) {
//response
console.log(r)
})
}
})
Run Code Online (Sandbox Code Playgroud)
有一个非常好的教程可以解释这个和其他相关的东西 - 提交AJAX表单:AngularJS方式.
基本上,您需要设置POST请求的标头以指示您将表单数据作为URL编码的字符串发送,并将要发送的数据设置为相同的格式
$http({
method : 'POST',
url : 'url',
data : $.param(xsrf), // pass in data as strings
headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload)
});
Run Code Online (Sandbox Code Playgroud)
请注意,此处使用jQuery的param()辅助函数将数据序列化为字符串,但如果不使用jQuery,也可以手动执行此操作.
小智 7
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
})
.error(function(){
});
Run Code Online (Sandbox Code Playgroud)
请结账! https://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs
| 归档时间: |
|
| 查看次数: |
390505 次 |
| 最近记录: |