【问题标题】:How can jQuery .load fail?jQuery .load 怎么会失败?
【发布时间】:2011-09-15 15:45:23
【问题描述】:

我正在使用一个简单的 ajax 加载器来获取 wordpress 上的内容。

$("#page_preview").load("ajaxloader/", function(response, status, xhr) {
      if (status == "error") {
        alert("Sorry but there was an error");
      }
      else
      {
          $('#page_preview').fadeIn(300);
      }
    });
return;

当我加载嵌入了谷歌地图的特定帖子时,显然出了点问题但是而不是进入if 语句,萤火虫显示它超出了这段代码。 ifelse 均未命中。

使用eclipse调试器发现页面加载成功,但是当它返回数据到.load()方法时,最后中断了。

对可能发生的事情有什么想法吗?

【问题讨论】:

  • 你能console.logalertresponse吗?
  • 如果在if 语句之前插入alert(status); 会发生什么?
  • 编辑了我的答案,让我知道您机器上“警报”的结果

标签: php jquery jquery-load


【解决方案1】:

如何

<script>
    // as of jQuery 1.5.x ++
    var pagePreview = $("#page_preview");
    $.get("ajaxloader/").success(
        function(response, status, jqXhr) {
            alert("Success!");
            pagePreview.empty();
            pagePreview.append(response);
            // i feel you need to parse the response for the google map div, and perform another $.get() of the google url?
        }).error(function(response, status, jqXhr) {
        alert("These are not the droids you are looking for. move along.");
    }).complete(function(response, status, jqXhr) {
        alert("Complete!");
    });
</script>

#为什么

jQuery.load() 将帮助您了解文档。 .load 等价于这个

它大致相当于 $.get(url, data, success) 只是它 是一种方法而不是全局函数,它有一个隐含的 回调函数。当检测到成功响应时(即当 textStatus 是“成功”或“未修改”),.load() 设置 HTML 匹配元素的内容到返回的数据。

您需要在$.ajax( complete:function(responseText, textStatus, XMLHttpRequest)){}); 注册并检查textStatus 的值。如果没问题,然后自己将数据加载到目标$('selector')。或者通过在 chrome (ctrl + shift +j) 中查看您的网络 xmlHttpRequests 或 firebug 中的某个网络选项卡来修复 .load(),并使用 $('selector').load( 'url', function(response, status, xhr){}) 中的“url”值调试问题

【讨论】:

    【解决方案2】:

    一种简单的调试方法是使用 firebug 控制台选项卡查看 XHR 请求。请求 URL 可能存在问题,您可能无法取回任何数据,或者您可能会收到错误或其他错误。使用 firebug,您可以轻松查看服务器返回的内容。

    由于 .load 仅在成功时加载,您可以添加一个

    console.debug("In .load function"); 
    

    在函数的请求下。再一次,用 firebug 检查你会发现 .load 回调是否真的被触发了。

    【讨论】:

      【解决方案3】:

      使用 $.ajax 函数 例如

          $.ajax({
            url: "test.html",
            context: document.body,
            success: function(){
              //when Successfully executed
            },
            error: function(){
              //When Error Fires
            }
          });
      

      【讨论】:

      • 这不能回答我的问题。我想知道 .load 怎么会失败。从理论上讲。
      猜你喜欢
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-12-07
      • 1970-01-01
      相关资源
      最近更新 更多