简体   繁体   中英

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

如您所说,您正在覆盖传递给$http的普通对象中的data密钥,将它们一起发送:

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

将其作为对象传递:

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

这使其工作正常:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM