Angular JS POST 请求不发送 JSON 数据 [英] Angular JS POST request not sending JSON data

查看:29
本文介绍了Angular JS POST 请求不发送 JSON 数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个对象作为 JSON 发送到我在 Flask 中的网络服务,该服务在请求数据中需要 JSON.

I am trying to send an object as JSON to my webservice in Flask that is expecting JSON in the request data.

我已经通过发送 JSON 数据手动测试了该服务,并且运行良好.但是,当我尝试通过 angular 控制器发出 http POST 请求时,Web 服务器向我发送一条消息,说它没有收到 JSON.

I have tested the service manually by sending JSON data and it works fine. However, when I try to make a http POST request through angular controller, the web server sends me a message saying it did not receive JSON.

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

When I inspect the request headers in Chrome it appears that data is not being sent in JSON but regular key/value pairs even through the Content Type is set to 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

如果您看到 Request Payload 下面的最后一行,您可以看到数据不是 JSON 格式.

If you seen the last line below Request Payload, you can see the data is not in JSON format.

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

This is the HTTP POST call in my angular controller:

$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 发送到服务器.

I am sending the data as an object {} but I have tried to send it after serializing by JSON.stringify however, nothing I do seems to send JSON to the server.

如果有人能帮忙,真的很感激.

Really appreciate if somebody can help out.

推荐答案

如果您正在序列化您的数据对象,它将不是一个正确的 json 对象.拿走你所拥有的,然后将数据对象包装在 JSON.stringify() 中.

If you are serializing your data object, it will not be a proper json object. Take what you have, and just wrap the data object in a 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;
});

这篇关于Angular JS POST 请求不发送 JSON 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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