www.webdeveloper.com
Results 1 to 5 of 5

Thread: How to read text file content located in some website with javascript or ajax?

Hybrid View

  1. #1
    Join Date
    May 2014
    Posts
    4

    How to read text file content located in some website with javascript or ajax?

    Hello. I have been trying to read text file content using javascript/ajax but I get no result. For example lets say that text file is located at http://somewebsite/data.txt

    I have been googleŽing around but it seems iŽm doing something wrong or solutions I have found are only suitable for reading local .txt files only.

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    if you are trying to test ajax on your computer, not via a server (web-facing: http://example.com/ or test/local server: http://[::1]/, http://localhost/, or http://127.0.0.1/). ajax will not work.

  3. #3
    Join Date
    May 2014
    Posts
    4
    so far my code is this:

    var result = "";

    $.ajax(
    {
    url: "http://somewebsite/data.txt", //IN MY CODE THE URL IS REAL ONE, THIS IS JUST EXAMPLE
    type: "GET",
    async: false,
    datatype: 'TEXT',
    success: function (data)
    {
    result = data;
    },
    error: function ()
    {
    alert("Oops! Something went wrong!");
    }
    });

    return result;

    I always get error and I do not know why...

  4. #4
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,001
    It is important to know that AJAX, currently, is a terrible cross-domain solution for anything (such as reading files or posting data).

    If you are trying to read a file from http://somewebsite/data.txt but the AJAX script is running from http://yoursite.com/ the AJAX will fail. The only exception to this is when a server specifically sets up cross-domain rules that will allow things like AJAX request to send or receive data. However if you don't control the site where this text file is located, then you are at the mercy of whoever does control the server. This also applies to running AJAX from your local computer (as ShrineDesigns mentioned), as your computer's domain/address does not match the domain where the file is located.

    That being said, in the event this text file is on your server and you are trying to read it from your server, then I would need to see your code to tell you why it doesn't work.

  5. #5
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    if you are able to make use of a server-side language like PHP. you can use ajax as normal, and when you encounter CORS issues, you can use a server-side script to fetch the request for the user and send it to the ajax.

    from the looks of your script i am guessing you are using jQuery or some sort of framework. for doing ajax i would recommend using purpose-built ajax request/response function(s). the (not so) brilliant minds at microsoft decided to leave out CORS in XmlHttpRequest object (back in IE 8 and 9, if i remember correctly). and thought it best to implement a "special-purpose" object called XDomainRequest. some of the functionality in XmlHttpRequest are present in XDomainRequest. One of the biggest benefits of XmlHttpRequest is sending/receiving requests in async mode. XDomainRequest does not have this feature, thus, using XDomainRequest will cause the page to hang for the duration of the request. you can circumvent this by using the server-side pass-thru idea.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles