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.

4
  • Which body is empty....request or response? What version of angular? Commented Dec 24, 2017 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. Commented Dec 24, 2017 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 Commented Dec 24, 2017 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. Commented Dec 24, 2017 at 21:35

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.