如何在 python django 中返回字典并在 javascript 中查看它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6467812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:47:18  来源:igfitidea点击:

how to return a dictionary in python django and view it in javascript?

javascriptpythondjango

提问by darren

I'm returning this in my view:

在我看来,我正在返回这个:

    data = {'val1' : 'this is x', 'val2' : True}
    return HttpResponse(data)

I want to use this information in the dictionary within my javascript. Kind of like this:

我想在我的 javascript 的字典中使用这些信息。有点像这样:

            function(data) {
                if (data["val2"]) {
                    //success
                    alert(data["val1"]);
                }
            }

However my javascript doesn't work. There is no alert popping up and I know that the dictionary has the information when it leaves my python view.

但是我的 javascript 不起作用。没有弹出警报,我知道字典在离开我的 python 视图时有信息。

How can I read this information in my JS?

我怎样才能在我的 JS 中阅读这些信息?



Ok so the answer for the view is to simplejson.dumps(data). Now when I do an alert(data) in my JS on my template I get {'val1' : 'this is x', 'val2' : True}. Now how can I manage the 2nd part of the question which is read out the values like

好的,所以视图的答案是 simplejson.dumps(data)。现在,当我在模板的 JS 中执行 alert(data) 时,我得到{'val1' : 'this is x', 'val2' : True}。现在我如何管理问题的第二部分,该部分读出的值如下

        function(data) {
            if (data["val2"]) {
                //success
                alert(data["val1"]);
            }
        }

UPDATE:The simplejson.dumps(data) converts th dictionary into string. So in the javascript you need to convert the string to an object. THis is the easiest but apparently unsafe way.

更新:simplejson.dumps(data) 将字典转换为字符串。因此,在 javascript 中,您需要将字符串转换为对象。这是最简单但显然不安全的方法。

var myObject = eval('(' + myJSONtext + ')');

回答by underrun

Very simply:

很简单:

import json
data = {'val1' : 'this is x', 'val2' : True}
return HttpResponse( json.dumps( data ) )

回答by levalex

JSON is easiest way to transfer data(also you can use XML).

JSON 是传输数据的最简单方法(您也可以使用 XML)。

In python:

在蟒蛇中:

    import json
    data = {'val1': "this is x", 'val2': True}
    return HttpResponse(json.dumps(data))

In javascript:

在 JavaScript 中:

    function (data) {
        data = JSON.parse(data);
        if (data["val2"]) {
            alert(data["val1"]);
        }
    }

回答by Sap

You can not directly use the python object you have to convert it into JSON string first Look into following documentation.

您不能直接使用 python 对象,您必须首先将其转换为 JSON 字符串查看以下文档。

http://docs.python.org/library/json.htmlalso http://www.json.org/

http://docs.python.org/library/json.html还有 http://www.json.org/

回答by Shwetabh Sharan

Just specify the mimetype in HttpResponse

只需在 HttpResponse 中指定 mimetype

    return HttpResponse(
                        json.dumps({"status":False, "message":"Please enter a report name."}) ,
                        content_type="application/json"
                        )