jquery - Null 不是 AJAX 的对象并检索部分页面

标签 jquery ajax

真的需要一些帮助来解决这个问题!

我尝试使用 jQuery 检索 HTML 的一部分,但出现错误:

Null is not an object on

此行导致错误:

$("#collection").html(jQuery(html).find('#collection').html());
function update_products(ajax_page) {  
  $.ajax({
    type: "GET",
    url: ajax_page,
    data: {},
    success: function(html) { 
      $("#collection").html(jQuery(html).find('#collection').html());  
      history.pushState({
        page: ajax_page
      }, "", ajax_page);                  
    }
  }); 
}

检索整个页面时它起作用:

function update_products(ajax_page) {  
  $.ajax({
    type: "GET",
    url: ajax_page,
    data: {},
    success: function(html) { 
      $("#collection").html(html);  
      history.pushState({
        page: ajax_page
      }, "", ajax_page);                  
    }
  }); 
}

我认为它与jQuery(html).find('#collection').html()有关,但我不知道哪里出了问题。

其他版本出错:

Null is not an object On this line `var $response=$(html);'

function update_products(ajax_page) {  
$.ajax({
url: ajax_page,
dateType: 'html',
success: function(html){ 
      //Create jQuery object from the response HTML.
    var $response=$(html);

    //Query the jQuery object for the values
    var collection = $response.filter('#collection').text();

  $("#collection").html(collection);  
     history.pushState({
     page: ajax_page
     }, "", ajax_page);                  
}


}); 

最佳答案

在之前的项目中,我做了类似以下的事情:

$("#collection").load(ajax_page+" #collection", ajax=true, function(){
    history.pushState({
        page: ajax_page
        }, "", ajax_page); 
});

当然,我的项目没有相同的 ID,并且我的 url 不在变量中,它是常量,所以我只是将其写入。

我在返回的数据中对我的项目做了进一步的工作,但我将其放入隐藏的 DIV 中,因此在我操作它之前它实际上是页面的一部分。这对你来说不起作用,因为 ID 会在页面上出现两次。没有它也许是可能的,但我并没有为了找出答案而奋斗。这些天我将进入 #collection div 的处理工作转移到一个单独的脚本中,这样当您想要刷新它时更容易使用 ajax。

关于jquery - Null 不是 AJAX 的对象并检索部分页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52367745/

相关文章:

javascript - 在php中使用jquery ajax删除从另一个页面获取的结果

javascript - 如何防止外部JS阻止你的网站加载?

jquery - AJAX 加载的内容会影响 SEO/搜索引擎吗?

javascript - Django 中的 Ajax 调用无需表单

javascript - onsubmit 返回 false 无效

jquery - 为什么第二个独立的ajax响应要等到第一个ajax响应才能获取?

javascript - node、sql 和 jquery -> 注册表单 - 访问服务器上的数据

javascript - Kendo UI 多选下拉菜单带有过滤器并选择所有选项

javascript - Leaflet setView by ajax (城市名称或邮政编码)

jquery - 使用淡入淡出显示/隐藏内容