agon024
agon024

Reputation: 1007

AngularJS - Posting data to API is not working

I am trying to send data that a user puts into a textarea and text input to and API that will save the data.

Here is the function:

$scope.forward = function() {

  $http({
    url: 'http://appsdev.pccportal.com:8080/ecar/api/reject/' + carID,
    method: "POST",
    data: "comments=" + this.comments,
    data: "recipient=" + this.recipient,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  }).
  then(function(response) {
    $scope.output = response.data;
  })

}

What it does when it is run is it logs only the recipient and not the comments. I am guessing because I am using "data" twice and it is only recognizing the last one (in this case "recipient"). How can I pass 2 values through this to the API.

Thanks

Upvotes: 0

Views: 33

Answers (3)

agon024
agon024

Reputation: 1007

This got it working just fine:

data: 'recipient='+encodeURIComponent(this.recipient)+'&comments='+encodeURIComponent(this.comments),

Upvotes: 0

martriay
martriay

Reputation: 5742

As you said, you're overwriting the data key from the plain object you're passing to $http, send it all together:

data: { recipient: this.recipient, comments: this.comments }

Upvotes: 2

HolyMoly
HolyMoly

Reputation: 2080

pass it as an object:

data : {comments: this.comments, recipient: this recipient}

Upvotes: 0

Related Questions