user1050619
user1050619

Reputation: 20866

loading a test file using AJAX

Im newbie to Ajax and need to load a test file contents from the server,

Here is my html page and views.py

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
        </script>
        <script>
            $(document).ready(function(){
                $("#test1").click(function(){
                    $("#test2").load("demo_test.txt");
                });
            });
        </script>
    </head>
    <body>
        <div id="test1">This is first block</div>
        <div id="test2">This is second block</div>
    </body>
</html>

views.py

def ajax_page(request):
    variables = RequestContext(request)
    return render_to_response('ajaxtest.html',variables)

urls.py

(r'^ajax/$',ajax_page),

When I run it and debug using the IE debugger, I get the msg,

"GET /ajax/demo_test.txt HTTP/1.1" 404 3295

Question: Can I get a file from the server a load it in test-2?..How can I specify the location of the file to the server?

Upvotes: 0

Views: 63

Answers (1)

Aamu
Aamu

Reputation: 3611

Please try this:

Create a view to return the demo_test.txt:

def demo(request):
    return render(request, 'demo_test.txt')

And create a url to fetch the above by the .load() method:

url(r'^demo/$', 'app.views.demo'),

In your script, pass the url to the .load() method:

$(document).ready(function () {
    $("#test1").click(function () {
        $("#test2").load(/demo/);
        return false;
    });
});

Upvotes: 1

Related Questions