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

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

问题描述

我想一个对象作为JSON发送到我的web服务在瓶被请求数据预期JSON。

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

我已经通过发送JSON数据手动测试服务,它工作正常。然而,当我试图让通过角控制器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

如果你看到下面的请求负载的最后一行,就可以看到数据不是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.

真的AP preciate如果有人可以帮忙。

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;
});

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

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