Is it possible to make an http request to an external domain by Xmlhttprequest in Javascript?

Asked

Viewed 138 times

-1

I’m working on my TCC and in it I planned to make a web service with PHP. It is hosted in a host external while the web page is on my Cotil-Unicamp localhost/served, until it’s walking, but now I’ve come across this, the XMLHttpRequest apparently won’t allow me to request another domain because of the same origin policy.

So I’d like to know if there’s anything I have to do to make the XMLHttpRequest allows the request or some other way to make requests in a way that can access external domains.

Yeah, I’ve already Googled, maybe I didn’t do it right since it’s my first time developing something like web service and I’m not as aware of the technical terms, so I apologize for the lack of experience.

I saw something like wear iframes hidden in HTML, but I thought I had kind of figured it out so I discarded.

1 answer

0


You need to allow the origin of the request, in php you could do something like:

<?php

header("Access-Control-Allow-Origin: *");
....
?>

You could also allow a specific source by swapping * for the domain. Since it’s local host * is the best output I think.

If you have control of the webserver where php is, you can set this header on the webserver itself:

Apache:

https://tecadmin.net/set-access-control-allow-origin-cors-in-apache/

NGINX

https://serverfault.com/questions/162429/how-do-i-add-access-control-allow-origin-in-nginx

  • So I had already done this, but javascript keeps showing me the same Warning of same origin policy, so I thought the problem was with the script, I really don’t know what’s wrong

  • Every time I ran into some Cors problem the solution was to release on the server.. vc can try to reach your php via Postman or in the browser’s own network panel to check if the header is coming. Another attempt, if you are not using a local webserver (Accessing the file directly://) is to implement a local webserver and test...

  • Now I understand, after you have talked about releasing on the host I went to search, and really the free Infinity, does not support this feature, I really did not know that I had this problem on the part of the server, error my excuse take up your time, but it was like I said I have no experience with it yet, and do you happen to know any host that would allow me to do it, for free? after all is just a TCC, anyway thank you!

  • @Lds20k, relax, we’re here to help. About the host none comes to mind... You could try the free-Ier from Amazon or Azure to try to solve... The bad side is that in both you will need to set up the server. It would not be feasible for you to host your JS on the same host or run your PHP program locally as well?

  • Yes, I saw what you had said up there, I had thought of this js option on the same host, but in my view it would escape the concept of web service and would become a js and php soup, sorry for my organizational craze... And as far as doing local, I had started developing it locally myself, but the problem is that I’m not doing TCC alone, I have three more companions with me, each developing a different front, for this issue that I found more reasonable to use on the web, I’ll take a look at these options you’ve passed, thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.