jQuery.getJSON 不触发回调

发布于 2024-07-14 03:57:37 字数 954 浏览 5 评论 0原文

我有一个 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 )但我没有找不到答案。

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

I have a html code:

<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), json variable 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#urldatacallback for example) but I didn't find an answer.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空气里的味道 2024-07-21 03:57:37

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

  • 使用 firebug 查看 JSON 响应

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

     $.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {    
            警报(文本状态); 
            警报(抛出错误); 
            警报(XMLHttpRequest.responseText); 
        }}); 
      

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

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

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:

  • 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);
      }});
    

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.ajax which has all of the available options for 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 Lint to validate it.

说谎友 2024-07-21 03:57:37

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

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

↙厌世 2024-07-21 03:57:37

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

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

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.

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

独自唱情﹋歌 2024-07-21 03:57:37

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文