【发布时间】: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