6

我正在使用一个简单的 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()方法时,最后中断了。

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

4

3 回答 3

4

如何

<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 为“success”或“notmodified”时),.load() 将匹配元素的 HTML 内容设置为返回的数据。

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

于 2011-09-15T16:12:35.297 回答
0

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

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

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

在功能的乞求。再一次,用 firebug 检查你会发现 .load 回调是否真的被触发了。

于 2011-09-16T06:31:53.413 回答
0

使用 $.ajax 函数,例如

    $.ajax({
      url: "test.html",
      context: document.body,
      success: function(){
        //when Successfully executed
      },
      error: function(){
        //When Error Fires
      }
    });
于 2011-09-25T08:20:53.713 回答