sma*_*ert 41 web-services http flask angularjs
我试图将一个对象作为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
Run Code Online (Sandbox Code Playgroud)
如果您看到Request Payload下面的最后一行,您可以看到数据不是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;
});
};
Run Code Online (Sandbox Code Playgroud)
我将数据作为对象{}发送,但我尝试在通过JSON.stringify序列化后发送它,但是,我没做什么似乎将JSON发送到服务器.
真的很感激,如果有人可以帮忙.
rGi*_*Gil 49
如果要序列化数据对象,则它不是正确的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;
});
Run Code Online (Sandbox Code Playgroud)
Her*_*Kan 10
我试过你的例子,它的工作正常:
var app = 'AirFare';
var d1 = new Date();
var d2 = new Date();
$http({
url: '/api/test',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
data: {application: app, from: d1, to: d2}
});
Run Code Online (Sandbox Code Playgroud)
输出:
Content-Length:91
Content-Type:application/json
Host:localhost:1234
Origin:http://localhost:1234
Referer:http://localhost:1234/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36
X-Requested-With:XMLHttpRequest
Request Payload
{"application":"AirFare","from":"2013-10-10T11:47:50.681Z","to":"2013-10-10T11:47:50.681Z"}
Run Code Online (Sandbox Code Playgroud)
您使用的是最新版本的AngularJS吗?
| 归档时间: |
|
| 查看次数: |
125006 次 |
| 最近记录: |