0

I want to call a web service with POST method this is the url

https://innovation.spa.uvm.mx:22580/webs-images/rest/imagx

i modified the url because of privacy of the company where i work, this is a example url but the original url has the same structure

I have done it in POSTMAN with this JSON string:

{
"personid" : "20369"
}

in POSTMAN it works fine i get a 200 OK status.

But in my ajax call i get a 500 error. net::ERR_ABORTED 500 Error. This is my ajax call:

$.ajax({
  url: "https://innovation.spa.uvm.mx:22580/webs-images/rest/imagx",
  type: "POST",
  dataType: 'json',
  contentType: 'application/json; charset=utf-8',
  data: JSON.stringify({
    "personid" : "20369"
  }),      
  success: function(result){
    console.log(JSON.stringify(result));
  },
  error: function(exception){
    alert("Error" + exception);
  }
});
  • You seams to be using `jsonp` but never specify a callback to use. If you don't need `jsonp`, try to remove the option. – Nicolas Jan 14 '20 at 17:56
  • 1
    @Nicolas — jQuery will dynamically generate the callback portion of the URL. – Quentin Jan 14 '20 at 17:59

1 Answers1

2

You aren't making a POST request, you are making a GET request.

JSONP can't make POST requests due to the way it works.

Presumably the webservice expects a POST request and errors because it isn't getting one.


Possibly you slapped jsonp in there in an effort to circumvent the Same Origin Policy. JSONP only does that if the web service you are making the request to explicitly supports JSONP (which it shouldn't these days because we have CORS (which gives a lot more control and doesn't have the security risks inherent in JSONP) now).


Even if you remove the jsonp, you are claiming contentType: 'application/json; charset=utf-8', but the value of data isn't JSON. If you pass an object to data then jQuery with URL encode it. If you want to send JSON you need to encode it yourself with JSON.stringify.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • why im making GET? im using jsonp because is a external url and if i dont use it i get this: has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. – Alex Palacios Jan 14 '20 at 18:04
  • @AlexPalacios — You are making a GET request for the reason I mentioned in the second paragraph of this answer. – Quentin Jan 14 '20 at 18:05
  • @AlexPalacios — The fourth paragraph of this answer said it was probably because you were trying to bypass the same origin policy. You can't do that that way. The server has to grant you permission. – Quentin Jan 14 '20 at 18:06