繁体   English   中英

ajax get json无法从Django接收数据

[英]ajax get json not receive data from django

我使用在ubutu上运行的django 1.4。 我正在尝试使用jquery / ajax来显示从django方法返回的数据。

我的档案views.py

def json_page(request):

    to_json = {
        "key1" : "value1",
        "key2" : "value2"
    }
    return HttpResponse(simplejson.dumps(to_json), mimetype="application/json;charset=UTF-8")

我的html文件:

    <script>
        $('document').ready(function() {     
            var url = "http://192.168.1.10:8000/json/";
            $.ajax({
                url: url,
                type: "GET",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                crossDomain: false,
                success: function(data) {
                    alert(data);
                },
                error: function(xhr, textStatus) {
                   console.log("error..");
                }
            });
        });
    </script>

我运行文件html,萤火虫返回状态= 200(确定)。 但不返回数据,返回情况:错误

你能帮我解决这个问题吗? 谢谢大家

试试这个代码。

<script>
            $('document').ready(function() {     
                var url ="http://192.168.1.10:8000/json/";
                $.ajax({
                    url: url,
                    type: "GET",

                    dataType: "json",

                    success: function(data) {
                        alert(success);
                        dat1=data.key1;
                        alert(dat1)
                    },
                    error: function(xhr, textStatus) {
                       console.log("error..");
                    }
                });
            });
        </script>

在你看来

def json_page(request):

        to_json = {
            "key1" : "value1",
            "key2" : "value2"
        }
        data=json.dumps(to_json)
        return HttpResponse(data, mimetype="application/json)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM