1

I'm using a ajax request for calling a REST service and I'm using post method to pass parameters. I get the response from the REST service when I pass the parameters in the REST URL, but when I send the parameters through data object I dont get any response. Am I doing anything wrong?

$.ajax({
  type: "POST",
  url: "myURL?ID=5087&name=hello",
     data:{
        'id':'5087',
        'name':'hello',

    },
  success: function(msg){
      alert('wow' + msg);
  }

});

In the above request If i remove the parameters from the URL and keep the data object as it is, I'm not getting any response

2
  • 'name':hello is looking for a variable hello that doesn't exist. Need to quote strings. Beyond that we don't know enough about how the api works Commented Jul 19, 2017 at 10:30
  • that was a mistake in the question. It is edited now Commented Jul 19, 2017 at 10:36

1 Answer 1

0

You're not doing anything wrong, the issue is that the API is not following semantic rules.

When receiving a POST request the data should be sent through the body of the request except for possibly an identifier which can be in the URL. For this reason, when you specify type: 'POST' and provide an object to data, that's where jQuery puts the information you send.

However, the API you're calling is retrieving the data you send in the POST request via the URL, when that should in fact be done with a GET request.

Due to this you will have to manually append the data to the URL in application/x-www-form-urlencoded format, as you are doing in the working example.

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

6 Comments

Could also be that post is expecting json contentType
Possible, but either way none of the values should be read from the querystring, so the request to myURL?ID=5087&name=hello should fail
Because if you set type: 'post' and use data jQuery will put the values in the body of the request, not the querystring
@RoryMcCrossan, Using the GET method with queryString doesn't return anything either. Is there any way I can use the POST method and date object to call the service apart from doing through appending?
That's my point - because of the bad design of the API you will have to use POST yet still manually build the querystring (or append the result of serialize() if you're using a form element)
|

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.