Angular JS POST request not sending JSON data

前端 未结 7 1700
忘掉有多难
忘掉有多难 2020-12-01 10:30

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

I have tested the service manually by sending JSON data a

相关标签:
7条回答
  • 2020-12-01 10:49

    I have tried your example and it works just fine:

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

    Output:

    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"}
    

    Are you using the latest version of AngularJS?

    0 讨论(0)
  • 2020-12-01 10:51

    you can use the following to find the posted data.

    data = json.loads(request.raw_post_data)

    0 讨论(0)
  • You can use FormData API https://developer.mozilla.org/en-US/docs/Web/API/FormData

    var data = new FormData;
    data.append('from', from);
    data.append('to', to);
    
    $http({
        url: '/path',
        method: 'POST',
        data: data,
        transformRequest: false,
        headers: { 'Content-Type': undefined }
    })
    

    This solution from http://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs

    0 讨论(0)
  • 2020-12-01 10:56
    $http({
        url: '/api/user',
        method: "POST",
        data: angular.toJson(yourData)
    }).success(function (data, status, headers, config) {
        $scope.users = data.users;
    }).error(function (data, status, headers, config) {
        $scope.status = status + ' ' + headers;
    });
    
    0 讨论(0)
  • 2020-12-01 10:58

    you can use your method by this way

    var app = 'AirFare';
    var d1 = new Date();
    var d2 = new Date();
    
    $http({
        url: '/api/apiControllerName/methodName',
        method: 'POST',
        params: {application:app, from:d1, to:d2},
        headers: { 'Content-Type': 'application/json;charset=utf-8' },
        //timeout: 1,
        //cache: false,
        //transformRequest: false,
        //transformResponse: false
    }).then(function (results) {
        return results;
    }).catch(function (e) {
    
    });
    
    0 讨论(0)
  • 2020-12-01 11:01

    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;
    });
    
    0 讨论(0)
提交回复
热议问题