asim choudhury
asim choudhury

Reputation: 63

Javascript XMlHttprequest to another domain

I am making a cross site http request using the Javascript XMlHttprequest api to send and retrieve data in post method. The purpose is to send sms using this service provided by the 3rd party. However I am not able to do, as this is not allowed by the browser and receiving the following error response "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://trans.smsfresh.co/api/sendmsg.php. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing)."

Is it possible to achieve this using html and javascript?

Upvotes: 4

Views: 5728

Answers (1)

sideshowbarker
sideshowbarker

Reputation: 88286

It’s not possible to get around it from the client side in your JavaScript/HTML.

The same-origin restrictions are enforced by your browser but can be relaxed by Access-Control-* CORS headers in the server response.

If a server doesn’t send an Access-Control-Allow-Origin response header for a resource, you won’t be able to fetch it from JavaScript in a Web app. No changes you make to your requests will fix that (e.g., no additional request headers you might add on the client side will help).

For details see the MDN HTTP access control (CORS) article.

And see Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not? etc here.

Upvotes: 6

Related Questions