小编典典

Angular JS POST请求未发送JSON数据

flask

我正在尝试将一个对象作为JSON发送到Flask中的Web服务,该对象期望请求数据中包含JSON。

我已经通过发送JSON数据手动测试了该服务,并且工作正常。但是,当我尝试通过角度控制器发出http POST请求时,Web服务器向我发送一条消息,说它未接收到JSON。

当我检查Chrome中的请求标头时,似乎不是以JSON格式发送数据,而是通过内容类型将常规键/值对设置为application / json

Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/plain, /
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:49
Content-Type:application/json;charset=UTF-8
DNT:1
Host:localhost:5000
Origin:http://localhost:5000
Referer:http://localhost:5000/
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
application=AirFare&d1=10-APR-2013&d2=14-APR-2013
如果您看到了请求有效载荷下面的最后一行,则可以看到数据不是JSON格式。

这是我的角度控制器中的HTTP POST调用:

$http({
url: ‘/user_to_itsr’,
method: “POST”,
data: {application:app, from:d1, to:d2},
headers: {‘Content-Type’: ‘application/json’}
}).success(function (data, status, headers, config) {
$scope.users = data.users; // assign $scope.persons here as promise is resolved here
}).error(function (data, status, headers, config) {
$scope.status = status + ‘ ‘ + headers;
});
};
我将数据作为对象{}发送,但是在JSON.stringify进行序列化之后,我尝试发送数据,但是,我似乎没有任何操作将JSON发送到服务器。

真的很感谢有人能帮忙。


阅读 570

收藏
2020-04-05

共1个答案

小编典典

如果你要序列化数据对象,那么它将不是正确的json对象。充分利用现有内容,然后将数据对象包装在中JSON.stringify()。

$http({
    url: '/user_to_itsr',
    method: "POST",
    data: JSON.stringify({application:app, from:d1, to:d2}),
    headers: {'Content-Type': 'application/json'}
}).success(function (data, status, headers, config) {
    $scope.users = data.users; // assign  $scope.persons here as promise is resolved here 
}).error(function (data, status, headers, config) {
    $scope.status = status + ' ' + headers;
});
2020-04-05