Search code examples
javascriptxmlhttprequestcors

Need to make a get request using javascript from a different domain


Hi I am trying to make a get request to fetch a json file and then edit it. The p ror. I was wondering if there is any way if I could Redirect to the host domain and then make the request from there to circumvent the issue. I know it would be easier to host my page from the domain itself, but unfortunately that is not an option.

function(){
   con = new XMLHttpRequest(); 
   con.open(url, false);
   con.send(); 
   const qjson = con.responseText(); 
   return qjson 
}

As I am running this from my local environment, due to domain difference - origin - http://localhost:port and target - https://differentdomain.com.

And I want to first navigate to the target domian and then make the request from there. Any suggestions are welcome.

p.s. - I am thinking of making a cors proxy server of my own for this purpose just wanted to make sure I explored all the alternatives before that.


Solution

  • I was wondering if there is any way if I could Redirect to the host domain and then make the request from there to circumvent the issue.

    No.

    JavaScript runs in the page it is in. If you navigate away from a page the execution environment the JS is running in goes away and the program stops.

    While it is possible to reach across windows and frames to pass data and call functions on other pages, the Same Origin Policy prevents you doing that across different domains.

    It would be a security nightmare if an attacker could cause your browser to navigate to your online banking website and then run JS of the attacker's creation.