jQuery.getJSON 不会触发回调

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/572991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:22:24  来源:igfitidea点击:

jQuery.getJSON doesn't trigger callback

jquerydjangogetjsonsimplejson

提问by chriss

I have a html code:

我有一个 html 代码:

<button>asd</button>
<script type = "text/javascript">
$('button').click(
    function() {
        $.getJSON('/schedule/test/', function(json) {
            alert('json: ' + json + ' ...');
        });
    }
);
</script>

and corresponding view:

和相应的观点:

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

The view is executed (tested using print), jsonvariable is initialised but no alert appears. What did I do wrong? I've already seen some docs on this (http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallbackfor example) but I didn't find an answer.

EDIT: The problem was, that HttpResponsewas not imported... Unfortunately Django gave no error about it. Everything else was correct. regards
chriss

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

编辑:问题是,那HttpResponse不是进口的……不幸的是,Django 没有给出任何错误。其他一切都是正确的。问候
克里斯

回答by jonstjohn

It is likely that the json is not properly formed. Sometimes this happens to me when my code, that should be producing json is generating an error. Two options:

很可能 json 格式不正确。有时,当我的代码应该生成 json 时会发生这种情况,但生成错误。两种选择:

  • Use firebug to view the JSON response

  • Setup error handling in your jquery code using the jQuery.ajaxSetup options such as:

      $.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
          alert(textStatus);
          alert(errorThrown);
          alert(XMLHttpRequest.responseText);
      }});
    
  • 使用 firebug 查看 JSON 响应

  • 使用 jQuery.ajaxSetup 选项在 jquery 代码中设置错误处理,例如:

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

Using the error handling for debugging is great, since you will know immediately when there is a problem with your response. You can check out the jQuery documentation for jQuery.ajaxwhich has all of the available options for jQuery.ajaxSetup.

使用错误处理进行调试非常棒,因为当您的响应出现问题时,您会立即知道。您可以查看jQuery.ajaxjQuery 文档,其中包含jQuery.ajaxSetup 的所有可用选项。

EDIT: A third option would be to just open the URL that should be generating the JSON and run the output through JSON Lintto validate it.

编辑:第三个选项是打开应该生成 JSON 的 URL 并通过JSON Lint运行输出以验证它。

回答by Andrew Bullock

Are you sure the JSON is valid? take a look at the response directly or use Firebug

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

回答by Paul

I ran into this a while back and rewrote a wrapper for jQuery's Ajax which allows you to pass the normal getJSON and an additional error callback per get.

不久前我遇到了这个问题,并为 jQuery 的 Ajax 重写了一个包装器,它允许您传递正常的 getJSON 和每个 get 的附加错误回调。

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

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

回答by Prabhat Gupta

I think you are missing the trailing $ in url pattern.

我认为您缺少 url 模式中的尾随 $ 。