[英]Django1.8 and Python2.7: Uncaught TypeError: Cannot read property '0' of undefined(…)
I'm using Django v1.8 in Python v2.7. 我在Python v2.7中使用Django v1.8。
I'm trying to read data from an API I created. 我正在尝试从我创建的API中读取数据。 The response of the API is in json format. API的响应为json格式。
The error I get is: ERROR -> Uncaught TypeError: Cannot read property '0' of undefined(…)
for this line of code (var key in response['alzDysphagia'][0] )
in an ajax call. 我得到的错误是: ERROR -> Uncaught TypeError: Cannot read property '0' of undefined(…)
在ajax调用中ERROR -> Uncaught TypeError: Cannot read property '0' of undefined(…)
此代码行的ERROR -> Uncaught TypeError: Cannot read property '0' of undefined(…)
(var key in response['alzDysphagia'][0] )
中的(var key in response['alzDysphagia'][0] )
。
Calling the API from browser itself, is working properly and I get the json response. 从浏览器本身调用API,工作正常,并且我得到json响应。
Can you help me please? 你能帮我吗?
Below you can find my views.py
code and the ajax call in my template. 在下面,您可以在模板中找到我的views.py
代码和ajax调用。
views.py 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)
template html file 模板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);
}
}
})**
Typo in your Python script: 在您的Python脚本中输入:
temp = json.dumps({"alzDyshpagia":[dict]})
Should be 应该
temp = json.dumps({"alzDysphagia":[dict]})
(the 'p' and 'h' are reversed). temp = json.dumps({"alzDysphagia":[dict]})
(“ p”和“ h”相反)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.