[英]How to send a rest PUT request in angular passing body
I need to send a rest request to a web server, I am using this code : 我需要将休息请求发送到Web服务器,我正在使用以下代码:
var getData = function(...) {
return $http({
method: "PUT",
url: getStatistics,
headers: {
'loginId': ...,
'token': ...,
'Content-Type': 'application/x-www-form-urlencoded',
},
data: {"...":"...","...":"...","...":"...","...":"...",
"...":"....","....":"...",
"datefrom":"2017-01-01","dateto":"2017-10-20"}
}).then(function(result){
var dataString = JSON.stringify(result.data);
alert(result.data);
return result.data;
});
};
The problem is that I am receiving error 400 bad request, What should I do? 问题是我收到错误400错误的请求,该怎么办?
The request works on Postman With this structure 该请求适用于具有此结构的邮递员
https://i.stack.imgur.com/BBrwF.jpg https://i.stack.imgur.com/BBrwF.jpg
https://i.stack.imgur.com/NbQuO.jpg https://i.stack.imgur.com/NbQuO.jpg
Should it be a problem of how Am I passing the data object ? 我应该如何传递数据对象应该是一个问题吗?
That's how I implemented the $httpParamSerializerJQLike, it it Correct? 这就是我实现$ httpParamSerializerJQLike的方式,对吗?
app.factory('statistics', function($http,$httpParamSerializerJQLike) {
var getData = function(loginId,token,id,dataInizio,dataFine,periodo,selezione) {
// Angular $http() and then() both return promises themselves
var data = '{"datefrom":"2017-01-01","dateto":"2017-10-20"}';
alert(data);
return $http({
method: "PUT",
url: getStatistics,
headers: {
'loginId': loginId,
'token': token,
'Content-Type': 'application/x-www-form-urlencoded',
},
data: { 'data': $httpParamSerializerJQLike(data)}
}).then(function(result){
var dataString = JSON.stringify(result.data);
alert(result.data);
return result.data;
});
};
return { getData: getData };
});
you're sending an x-www-form-urlencoded body, but it contains a single parameter named data, containing JSON. 您正在发送x-www-form-urlencoded正文,但它包含一个名为data的单个参数,其中包含JSON。
You thus need 因此,您需要
data: $httpParamSerializerJQLike({data: angular.toJson(data)})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.