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.
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.
so far my code is this:
var result = "";
url: "http://somewebsite/data.txt", //IN MY CODE THE URL IS REAL ONE, THIS IS JUST EXAMPLE
success: function (data)
result = data;
error: function ()
alert("Oops! Something went wrong!");
I always get error and I do not know why...
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.
"Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"
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.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)