我有一个 getJSON() 调用,它访问一个返回一些数据的视图。调用有效并检索了数据,但尽管如此,我仍然收到 500 错误。
Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)
MultiValueDictKeyError at /album_ajax/
"Key u'reid' not found in <QueryDict: {}>"
Request Method: GET
Request URL: http://127.0.0.1:8000/album_ajax/
Django Version: 1.4.3
Python Executable: /usr/bin/python
Python Version: 2.7.1
Python Path: ['/Users/Santi/programming/feastfm', '/Library/Python/2.7/site-packages/pip-1.2.1-py2.7.egg', '/Library/Python/2.7/site-packages/South-0.7.6-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages']
我查看了 SO 并尝试在 IRC 上四处询问,但无法弄清楚为什么会出现此错误。视图/调用的语法直接复制自: http: //lethain.com/two-faced-django-part-5-jquery-ajax/
视图.py
def get_album_tracks(request):
get = request.GET.copy()
reid = get["reid"]
url = "http://www.musicbrainz.org/ws/2/release/"+reid+"?fmt=json&inc=artist-credits+recordings"
data = urllib2.urlopen(url)
api_results = json.load(data)
tracks = []
for entry in api_results['media']:
for track in entry['tracks']:
artist = track['artist-credit'][0]['name']
title = track['title']
tracks.append({'artist':artist, 'title':title})
# results = {'reid':reid, 'tracks':tracks}
results = {'test':"is this working?", "reid":reid, 'tracks':tracks}
return_json = simplejson.dumps(results)
return HttpResponse(return_json, mimetype='application/json')
getJSON() 调用
$.getJSON("/album_ajax/", {"reid": reid}, function(json){
if(json['tracks']){
//alert("json?: " + json["tracks"]+" reid: "+json['reid']);
// var album = $('data-reid ='+)
for(var i=0; i<json['tracks'].length; i++){
//console.log(json['tracks'][i]['title'])
var artist = json['tracks'][i]['artist']
var title = json['tracks'][i]['title']
$(ul).append("<li class = 'album-track track' data-artist = '"+escape(artist) +"' data-title ='" + escape(title) +"'>"+artist +" - "+ title+"</li>");
}
}
else{
alert('no results')
}
})
我不明白为什么我会同时收到 500 错误并运行成功功能。
编辑
我在视图中放置了一个打印语句,看起来视图函数被调用了两次,一次是空的 QueryDict,一次是包含 reid 变量的 QueryDict:
album_tracks was just called
<QueryDict: {}>
album_tracks was just called
<QueryDict: {u'reid': [u'0447570d-4804-49f1-9396-d71ddd8f59c4']}>
我仍然不确定在代码中的哪个位置进行了两次调用。
编辑 2
包括具有 getJSON() 调用的整个函数
$('.album').click(function(e){
var $target = $(e.target);
// if(!$target.is("ul"))
if(!$target.is("div")) //magic happens here!!
{
return;
}
if($(this).children("li").length >= 1){
$(this).children("li").toggle();
}
else{
var reid = $(this).data('reid');
ul = this;
var data = {"reid": reid};
// var args = { type:"GET", url:"/album/", data:data, complete:done };
// $.ajax(args);
$.getJSON("/album_ajax/", data, function(json){
if(json['tracks']){
//alert("json?: " + json["tracks"]+" reid: "+json['reid']);
// var album = $('data-reid ='+)
for(var i=0; i<json['tracks'].length; i++){
//console.log(json['tracks'][i]['title'])
var artist = json['tracks'][i]['artist']
var title = json['tracks'][i]['title']
$(ul).append("<li class = 'album-track track' data-artist = '"+escape(artist) +"' data-title ='" + escape(title) +"'>"+artist +" - "+ title+"</li>");
}
}
else{
alert('no results')
}
})
}
});