AJAX request for REST service in Jquery, Getting r

2019-07-25 08:15发布

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

1条回答
孤傲高冷的网名
2楼-- · 2019-07-25 08:52

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.

查看更多
登录 后发表回答