0

我是 Ajax 的新手,需要从服务器加载测试文件内容,

这是我的html页面和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>

视图.py

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

网址.py

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

当我运行它并使用 IE 调试器进行调试时,我得到了消息,

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

问题:我可以从服务器获取文件并在 test-2 中加载它吗?..如何将文件的位置指定到服务器?

4

1 回答 1

1

请试试这个:

创建一个视图以返回 demo_test.txt:

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

并创建一个 url 以通过 .load() 方法获取上述内容:

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

在您的脚本中,将 url 传递给 .load() 方法:

$(document).ready(function () {
    $("#test1").click(function () {
        $("#test2").load(/demo/);
        return false;
    });
});
于 2013-11-12T14:53:51.303 回答