【问题标题】:Django1.8 and Python2.7: Uncaught TypeError: Cannot read property '0' of undefined(…)Django1.8 和 Python2.7:未捕获的 TypeError:无法读取未定义的属性“0”(…)
【发布时间】:2017-04-25 22:32:05
【问题描述】:

我在 Python v2.7 中使用 Django v1.8。

我正在尝试从我创建的 API 中读取数据。 API的响应为json格式。

我得到的错误是:ERROR -> Uncaught TypeError: Cannot read property '0' of undefined(…) for this line code (var key in response['alzDysphagia'][0] ) in an ajax call。

从浏览器本身调用 API,工作正常,我得到 json 响应。

你能帮帮我吗?

您可以在下面找到我的 views.py 代码和模板中的 ajax 调用。

views.py

for myfields in l_v:
        if counter ==0:
            print "counter=0"
            counter = counter +1
        elif counter !=0:
            if counter == count-1:
                myfields = '{'+myfields
                dictionary = json.loads(myfields)

                dict= {"DysphagiaComment" : dictionary["DysphagiaComment"] , "Imipaxireusta" : dictionary["Imipaxireusta"] , "Liquids":dictionary["Liquids"] , "Solid":dictionary["Solid"] }

                print "dict "
                print dict

                print "dictionary"
                # print dictionary

            else:
                counter += 1

# print form_collection
print "diction"
# print dictionary

# temp = json.dumps(dict)
temp = json.dumps({"alzDyshpagia":[dict]})
print temp

return HttpResponse(temp)

模板html文件

**$.ajax({
                            url: "/Dysphagia",
                            type: "post",
                            data: {
                                csrfmiddlewaretoken: '{{ csrf_token }}',
                                patientId: aValue,
                                Select_dys: true
                            },
                             dataType:'json',
                            async: true,
                            success: function (response){
                                console.log("response :");
                                console.log(response.type);
                                console.log("rw");
                                for (var key in response['alzDysphagia'][0] ){
{#                                    console.log(response['alzDysphagia'][0][key]);#}
                                    $('[name="' + key.toLowerCase() + '"]input[value="' + response['alzDysphagia'][0][key] + '"]').prop('checked', true)
                                    $("#" + key.toLowerCase()).val(response['alzDysphagia'][0][key]).attr('disabled',true);

                            }

                            }

                        })**

【问题讨论】:

    标签: javascript ajax django python-2.7


    【解决方案1】:

    Python 脚本中的错字:

    temp = json.dumps({"alzDyshpagia":[dict]})

    应该是

    temp = json.dumps({"alzDysphagia":[dict]})(“p”和“h”颠倒了)。

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 2016-11-17
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      • 2019-05-16
      • 2018-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多