jQuery.getJSON不会触发回调
发布时间:2020-12-16 19:47:25 所属栏目:百科 来源:网络整理
导读:我有一个html代码: buttonasd/buttonscript type = "text/javascript"$('button').click( function() { $.getJSON('/schedule/test/',function(json) { alert('json: ' + json + ' ...'); }); });/script 并相应视图: def test(request): if request.metho
我有一个html代码:
<button>asd</button> <script type = "text/javascript"> $('button').click( function() { $.getJSON('/schedule/test/',function(json) { alert('json: ' + json + ' ...'); }); } ); </script> 并相应视图: 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),但我没有找到答案。
这可能是json没有正确形成。有时这发生在我的时候,我的代码,应该是生产的json生成错误。两个选项:
>使用firebug查看JSON响应 $.ajaxSetup({"error":function(XMLHttpRequest,textStatus,errorThrown) { alert(textStatus); alert(errorThrown); alert(XMLHttpRequest.responseText); }}); 使用错误处理调试是伟大的,因为你会知道,当你的响应有问题。你可以查看jQuery documentation for jQuery.ajax,它有jQuery.ajaxSetup的所有可用选项。 编辑:第三个选项是只打开应该生成JSON的URL,并通过JSON Lint运行输出来验证它。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |