2016-12-10 95 views
0

我在Python v2.7中使用Django v1.8。Django1.8和Python2.7:未捕获TypeError:无法读取属性'0'undefined(...)

我想从我创建的API中读取数据。 API的响应采用json格式。

我得到的错误是:ERROR -> Uncaught TypeError: Cannot read property '0' of undefined(…)对于这行代码(var key in response['alzDysphagia'][0])在ajax调用。

从浏览器本身调用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); 

          } 

          } 

         })** 

回答

1

错字在你的Python脚本:

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

应该

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

相关问题