Question

I was trying to load content/text of the div from the text file on the server at same location of the webpage and it is not working.

Here is a code I have written to do this...

HTML File

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("button").click(function () {
                alert('loading data');
                $("#container").load("Content.txt");
            });
        });
    </script>   
</head>
<body>
    <div id="container">
        Test
    </div>

    <button>Get External Content</button>
</body>
</html>

Here is a text file content : Content.txt

bla, bla bla bla etc etc

The div is not populating with the text file content... Any solution?

Finally I put some valid HTML text into text file and it did work, but, in my case the text in the text file will be the code samples and I want to populate the code as text of div from text file on the server when page is loading.

Any suggestion with code sample will be much appreciated. jQuery samples or JavaScript will be okay.

Thanks & Regards!!

Was it helpful?

Solution

My guess is that you need to set the ajax dataType, like so:

$(document).ready(function () {
    $("button").click(function () {
        $.ajax({
            url : "Content.txt",
            dataType: "text",
            success : function (result) {
                $("#container").html(result);
            }
        });
    });
});

It's supposed to detect the data type, but perhaps there's not enough to go on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top