jQuery.getJSON不会触发回调

我有一个HTML代码:

  $('button').click( function() { $.getJSON('/schedule/test/', function(json) { alert('json: ' + json + ' ...'); }); } );  

和相应的观点:

 def test(request): if request.method == 'GET': json = simplejson.dumps('hello world!') return HttpResponse(json, mimetype = 'application/json') 

执行视图(使用print测试),初始化json变量但不显示警报。 我做错了什么? 我已经看过一些关于此的文档(例如http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback ),但我没有找到答案。

编辑:问题是, HttpResponse没有导入…不幸的是Django没有给出任何错误。 其他一切都是正确的。 问候
CHRISS

json很可能没有正确形成。 有时这种情况发生在我的代码中,应该生成json会产生错误。 两种选择:

  • 使用firebug查看JSON响应

  • 使用jQuery.ajaxSetup选项在jquery代码中设置error handling,例如:

      $.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) { alert(textStatus); alert(errorThrown); alert(XMLHttpRequest.responseText); }}); 

使用error handling进行调试很有用,因为当您的响应出现问题时,您会立即知道。 您可以查看jQuery.ajax的jQuery文档,其中包含jQuery.ajaxSetup的所有可用选项。

编辑:第三种选择是打开应生成JSON的URL并通过JSON Lint运行输出以validation它。

你确定JSON有效吗? 直接查看响应或使用Firebug

我回过头来重新编写了一个jQuery的Ajax包装器,它允许你传递正常的getJSON和每个get的额外错误回调。

http://www.nurelm.com/themanual/2010/08/09/self-indulgent-code-jquery-getjson-with-error-handling/

我认为你错过了url模式中的尾随$。