简体   繁体   English

为什么不只在一页上调用我的回调?

[英]Why isn't my callback being called on one page only?

I am using jQuery to grab some JSON and then plug it into some elements and display it on my page. 我正在使用jQuery来获取一些JSON,然后将其插入某些元素并显示在我的页面上。

It works fine on all pages except one , where the response seems to be the page itself. 它在除第一个 页面之外的所有页面可以正常工作,其中响应似乎是页面本身。

I have placed alert() s in the callbacks ( success and complete ) and they never seem to be fired (though Firebug shows the request returning 200 OK which should trigger the success handler). 我已经将alert()放置在回调中( successcomplete ),它们似乎从未被触发(尽管Firebug显示请求返回200 OK的请求,这应该触发success处理程序)。

I don't know what to do, I've never encountered this before. 我不知道该怎么办,我以前从未遇到过。

Here is the jQuery code I am using: 这是我正在使用的jQuery代码:

var specials = (function() {

    var specials = false,
        specialsAnchor;

    var init = function() {
        specialsAnchor = $('#menu-specials a');

        specialsAnchor.click(function(event) {
            event.preventDefault();
            if (specials != false && specials.is(':visible')) {
                hide();
            } else {
                show();
            }

        });
    };

    var load = function(callback) {

      specialsAnchor.addClass('loading');

      specials = $('<div />', { 'id': 'specials' }).hide().appendTo('#header');

      var specialsUl = $('<ul />').appendTo(specials);

      $.ajax(specialsAnchor.attr('href'), {
          dataType: 'json',
          success: function(data) {

            $.each(data, function(i, special) {

                specialsUl.append('<li><h4>' + special.heading + '</h4><p>' + special.content + '</p></li>');

            });
            specialsAnchor.removeClass('loading');
            callback.call();

         }

      });

    }

    var show = function() {
      if (specials == false) {
        load(show);
        return;
      }
      specials.slideDown(500);
    }

    var hide = function() {
      specials.slideUp(500);
    }

    $(init);

})();

What is going on? 到底是怎么回事?

I noticed that you're including jquery.validate on this page, but not the others. 我注意到您在此页面上包括了jquery.validate,但其他页面没有。 jQuery validate with jQuery > 1.5 causes some issues with AJAX calls. jQuery使用jQuery> 1.5进行验证会导致 AJAX调用出现一些问题

I realize the linked question/answer aren't exactly what you're seeing, but I've seen all kinds of weird issues with AJAX calls and this combination of validate and jQuery, so I figured it would be worth mentioning. 我意识到链接的问题/答案并不完全是您所看到的,但是我已经看到AJAX调用以及validate和jQuery的这种组合产生的各种奇怪问题,因此我认为值得一提。

Hope that helps. 希望能有所帮助。

This is probably not a complete answer, but could be a step in the right direction. 这可能不是一个完整的答案,但可能是朝正确方向迈出的一步。 Using Charles Proxy it seems on your other pages when I click specials, it makes a request to http://www.toberua.com/~new/specials however on the contact-us page the ajax request is instead going to http://www.toberua.com/~new/contact-us (which of course is not json) 使用Charles Proxy时,当我单击特价商品时,它似乎出现在您的其他页面上,它向http://www.toberua.com/~new/specials发出了请求,但是在联系我们页面上,ajax请求却转到了http:/ /www.toberua.com/~new/contact-us (当然不是json)

One other interesting note: The XMLHttpRequest on other pages sets the Accept header properly (ie Accept application/json, text/javascript, */*; q=0.01 , whereas on the contact-us page it is set to Accept */* ). 另一个有趣的注释:其他页面上的XMLHttpRequest正确设置了Accept标头(即Accept application/json, text/javascript, */*; q=0.01 ,而在与我们联系的页面上将其设置为Accept */* ) 。 I'd bet there's a different code branch being invoked... 我敢打赌,有一个不同的代码分支被调用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM