Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to load and parse file on remote server?

Is it possible to download and parse a plain text file from different domain with JavaScript?

I've got this fiddle so far, but I'm stuck figuring out what I'm doing wrong.

Markup:

  <div id="clickme">Click me</div>
  <div id="result">Result: </div>

Code:

$("#clickme").click(function() {
  /* ###################################
     NOTE: im on say example.com/test.html but trying
     NOTE: to access different_domain_sample.com
   */
  var req = new XMLHttpRequest();
  var sURL = "http://www.google.com/robots.txt";

  req.open("GET", sURL, true);
  req.setRequestHeader("User-Agent", "blah/4.2");

  req.onreadystatechange = function() {
    if (req.readyState == 4) {
        $("#result").text("Result is: <pre>" + req.responseText + "</pre>");
    }
  };
  req.send(null);
});

Already answered, but more info on this is here Cross-origin resource sharing

like image 469
lzdt Avatar asked Nov 26 '25 06:11

lzdt


1 Answers

It's because of browsers have implemented a feature called cross-site-scripting-prevention. You could for example do the ajax request on a php file on the same server and in that query the target page using curl.

like image 67
Andreas Linden Avatar answered Nov 27 '25 20:11

Andreas Linden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!