Note that there are some explanatory texts on larger screens.

plurals
  1. POgetJSON call succeeds but still throws 500 error
    primarykey
    data
    text
    <p>I have a getJSON() call that accesses a view that returns some data. The call works and the data is retrieved but despite this I am still getting a 500 error.</p> <pre><code>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 &lt;QueryDict: {}&gt;" 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'] </code></pre> <p>I've looked on SO and tried asking around on IRC but haven't been able to figure out why I'm getting this error. The syntax of the view/call is copied directly from: <a href="http://lethain.com/two-faced-django-part-5-jquery-ajax/" rel="nofollow">http://lethain.com/two-faced-django-part-5-jquery-ajax/</a></p> <p>views.py</p> <pre><code>def get_album_tracks(request): get = request.GET.copy() reid = get["reid"] url = "http://www.musicbrainz.org/ws/2/release/"+reid+"?fmt=json&amp;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') </code></pre> <p>the getJSON() call</p> <pre><code>$.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&lt;json['tracks'].length; i++){ //console.log(json['tracks'][i]['title']) var artist = json['tracks'][i]['artist'] var title = json['tracks'][i]['title'] $(ul).append("&lt;li class = 'album-track track' data-artist = '"+escape(artist) +"' data-title ='" + escape(title) +"'&gt;"+artist +" - "+ title+"&lt;/li&gt;"); } } else{ alert('no results') } }) </code></pre> <p>I don't understand why am I getting both a 500 error and having the success function run.</p> <p><strong>EDIT</strong></p> <p>I put a print statement inside my view and it looks like the view function is being called twice, once with an empty QueryDict and once with a QueryDict containing the reid variable:</p> <pre><code>album_tracks was just called &lt;QueryDict: {}&gt; album_tracks was just called &lt;QueryDict: {u'reid': [u'0447570d-4804-49f1-9396-d71ddd8f59c4']}&gt; </code></pre> <p>I am still not sure where in the code the call is made twice. </p> <p><strong>EDIT 2</strong></p> <p>Including the entire function which has the getJSON() call</p> <pre><code>$('.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 &gt;= 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&lt;json['tracks'].length; i++){ //console.log(json['tracks'][i]['title']) var artist = json['tracks'][i]['artist'] var title = json['tracks'][i]['title'] $(ul).append("&lt;li class = 'album-track track' data-artist = '"+escape(artist) +"' data-title ='" + escape(title) +"'&gt;"+artist +" - "+ title+"&lt;/li&gt;"); } } else{ alert('no results') } }) } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload