Can I upload and analyze a file on a remote server?

Is it possible to download and parse a text file from another domain using JavaScript?

I have this fiddle , but I'm stuck figuring out what I'm doing wrong.

Markup:

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

the 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 here Resource Sharing

+3
source share
2 answers

There are three ways to do this:

  • Using a non-browser-based proxy from your domain, which will receive data on your behalf. You can also use a plugin that can bypass a policy of the same origin.
  • JSONP . , - JSONP.
  • ( , )
+3

- , , . , ajax php curl.

+4

All Articles