How To Display A .txt File In Jquery
Solution 1:
Due to browser security restrictions, most Ajax requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, port, or protocol.
I'm assuming the .txt file is not on the same server/domain as the HTML file that is trying to load it? If you are, then you should use a relative URL, i.e.:
$('#text').load("xe7/user.txt");
Please refer to: Loading cross domain endpoint with jQuery AJAX
Solution 2:
Does this work?
jQuery
$(function(){
$.ajax({
url : "http://hokuco.com/test/xe7/user.txt",
dataType: "text",
success : function (data) {
$("#text").html(data);
}
});
});
HTML
<div id="text"></div>
Solution 3:
As other users have said, you are violating the same origin policy. If you are using PHP you could echo that into your textarea.
Something along the lines of:
<textareaid="text"><?php$text = file_get_contents('http://hokuco.com/test/xe7/user.txt');
echo$text;
?></textarea>
Solution 4:
If the above code is all there is (as you state in your comment to your question), and you have included jQuery, then mere refreshing of the page won't give you the result. As your HTML code is below this jQuery call, the DOM is not ready yet when the function is called. Therefore you need to call this function when the document is ready, according to the rule that all jQuery calls that relate to DOM elements should be made this way:
$(document).ready(function(){
$('#text').load("test/xe7/user.txt");
});
Post a Comment for "How To Display A .txt File In Jquery"