1

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.

hackerman
  • 1,221
  • 3
  • 17
  • 38
  • Which body is empty....request or response? What version of angular? – charlietfl Dec 24 '17 at 20:06
  • Unless I’m misunderstanding there is no request/response, it’s not an API, but a webhook. The POST received by the webhook is empty. HTTP 200 is successfully returned and the webhook does receive the POST, it just has an empty body. – hackerman Dec 24 '17 at 20:10
  • Seems you are misunderstanding. `$http` makes an ajax request and sends your data in the request body and waits for the response. If the response body is empty that is an api issue – charlietfl Dec 24 '17 at 20:13
  • 1
    Check your Developer Console for errors. `Failed to load https://requestb.in/18z1tu41: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://run.plnkr.co' is therefore not allowed access.` Which is a CORS problem. – georgeawg Dec 24 '17 at 21:35

1 Answers1

1

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

Henri Benoit
  • 705
  • 3
  • 10