AJAX POST request working in POSTMAN but not in Ch

2019-06-06 03:50发布

问题:

I am trying to make a simple AJAX POST request. This request when executed in POSTMAN works fine. However, when I code it in jQuery and try to make the same request, I get

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'file://' is therefore not allowed access. The response had HTTP status code 415.

Now, I have to make a POST request and hence using JSONP as type in the request won't help. I do not have access to server code to modify it. My basic understanding is that if POSTMAN can execute it, then i should be able to do it as well

回答1:

the status code is self explanatory. 415 it's returned from the server when you sent an unsupported content type (e.g. an xml). double check the type of data you're passing to the post request in your code.

415 UNSUPPORTED MEDIA TYPE The origin server is refusing to service the request because the payload is in a format not supported by this method on the target resource.

The format problem might be due to the request's indicated Content-Type or Content-Encoding, or as a result of inspecting the data directly.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/415



回答2:

I doesn't automatically work in the browser if it works in Postman. You can still get CORS-errors.

You might also check the content type, you are getting HTTP STATUS 415 which is UNSUPPORTED MEDIA TYPE . This might be your problem, the media type differs in the request.

I also noticed you are serving from file://, maybe you should host it so you are serving your site from localhost instead.



回答3:

Ok, so the issue is you are running your page from a file directly(May be just double clicking on the file in windows) rather using an server.

So first of all access the page via server, install any server like apache, and access the page via

http://localhost/pagename