AngularJS [SOLVED]: Why is HTTP POST returning empty in this AngularJS code?

AngularJS [SOLVED]: Why is HTTP POST returning empty in this AngularJS code?

Home Forums Frameworks AngularJS tutorials AngularJS [SOLVED]: Why is HTTP POST returning empty in this AngularJS code?

Tagged: 

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246232

    Cloudy Point
    Keymaster

    QuestionQuestion

    I am using my AngularJS code below to POST to a webhook. A POST request does go through to the webhook. However, it has no body, so none of the data is being returned.

        $scope.addguest = function(guest){
        //todo put guest to url
        url = "https://requestb.in/18z1tu41";
        item = {
        'property_id':$scope.id,
        'originator':guest.phone,
        'guest_name':guest.name,
        'check_out_date':guest.date
        }
        $('#exampleModalPhone').modal('hide');
        $http.post(url, item, {headers: {'Content-Type': 'application/json'} }).success(function (data) {
               $scope.data= data;
           });
    

    Am I missing something here with how my code is written? What is causing the POST to be empty?

    This is not a CORS related issue, so not a duplicate.

    #246233

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Not too sure in which language the service processing the POST request is written but in some languages (e.g. PHP) you should rather use a different encoding to avoid issues:

    $http.post(url,  $.param(item), {headers: {'Content-Type':'application/x-www-form-urlencoded'} })
    

    $.param will convert your object to a URL parameter formatted key/value pair

    Source: https://stackoverflow.com/questions/47963673/why-is-http-post-returning-empty-in-this-angularjs-code
    Author: benohead
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.