0

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

1
  • concat the data into one string, separated by &. don't forget to encodeURIComponent... Commented Aug 17, 2016 at 23:01

3 Answers 3

2

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 }
Sign up to request clarification or add additional context in comments.

7 Comments

This does not post the data to the server through the API the only thing that works is " data: "comments=" + this.comments " not " comments: this.comments "
@agon024 why do you say so? By the way, success is deprecated, use then insted. See: docs.angularjs.org/api/ng/service/$http
Because the response I get back from the server is "please enter an email address that exists on the server"..and the network response for the form is {"recipient":"[email protected]","comments":"test"}. Instead of recipient:[email protected], comments:test, And on my other controllers that hit the server with only using comments textbox it posts to the server with a response of comments: test and the data is saved. And the only way its saves is with data: "comments=" + this.comments. So the info is not saved in the DB.
@agon024 well, this is odd. Are you sure that the problem is on how do you send the params? Look in the link I posted before that this is a valid way to send them, also the error you pasted says only that the received email is not in the database. Why don't you print the received params on the server side? I think the problem is there
{"recipient":"[email protected]","comments":"test"} is what is coming back when it should be recipient: [email protected], comments: test.
|
0

pass it as an object:

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

1 Comment

As i posted below, this does not post the data to the server through the API the only thing that works is " data: "comments=" + this.comments " not " comments: this.comments "
0

This got it working just fine:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.