-1

I'm looking to get the content of a text file in Javascript to parse it after. I know how to do it with Jquery, i used to call the ajax function with JSONP, but, this time i would like to do it without any framework.

I did code a lot of try but nothing succeed. Here is the last version :

var url = 'https://url.com/videosList.txt';

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = process;
xhr.open("GET", url, true);
xhr.send();

function process()
{
  if (xhr.readyState == 4) {
    console.log(xhr.responseText);
  }
}

The code seems to be ok but my browser (chromium) returns me :

> XMLHttpRequest cannot load
> https://url.com/videosList.txt. Origin
> http://localhost:8888 is not allowed by Access-Control-Allow-Origin.
Xavier
  • 3,919
  • 3
  • 27
  • 55

1 Answers1

1

Cross-Site Scripting is not allowed under default security settings.. Your code would work if the requested URL were on the same domain the page came from.

If you have control of the remote server, you can add a header to the response:

Access-Control-Allow-Origin: *

But XMLHttpRequest is (or used to be) different, between different browsers (mainly IE). So that means you need a "library shim", to unify your code across that inconsistency.. I'd just stick with jQuery if possible :)

Here are some more references:

http://www.cypressnorth.com/blog/programming/cross-domain-ajax-request-with-json-response-for-iefirefoxchrome-safari-jquery/

Happy trails.

Thomas W
  • 13,940
  • 4
  • 58
  • 76