简体   繁体   English

jQuery Ajax从站点的另一个页面加载内容

[英]jQuery Ajax loading content from another page of the site

I'm trying to pull the contents of Div from another page of the site I'm working on and update the Div on the current page. 我正在尝试从我正在处理的网站的另一页面上提取Div的内容,并在当前页面上更新Div。 I want to do this so I don't need a page reload. 我想这样做,所以不需要重新加载页面。 The Ajax call I'm making works fine in Firefox, it updates the Div with the content from the other page of the site. 我正在进行的Ajax调用在Firefox中工作正常,它使用站点另一页面上的内容更新了Div。 The problem is it breaks in IE, when I try to get the HTML from the data being returned using .html() I get null in IE. 问题是它在IE中损坏,当我尝试从使用.html()返回的数据中获取HTML时,我在IE中得到了null。 If I do an alert just on the data being returned, the html shows up. 如果我仅对返回的数据进行警报,则显示html。

var divItems = $("#tab-bbp a").attr("href");
$.ajax({
   type: "GET",
   url: divItems,
   dataType: "html",
   success: function(msg){
       $("#persistenslcont").html($("#persistenslcont",msg).html());
       //null in IE, returns HTML in FF
       alert($("#persistenslcont",msg).html());
   },
   error: function () {
       alert("error");
}                   
});

It seems odd this doesn't work in IE. 看来这在IE中不起作用很奇怪。 Also, I'm not sure if the is the best technique to do what I am trying to accomplish. 另外,我不确定做是否是我要完成的最佳技术。 Any help would be appreciated. 任何帮助,将不胜感激。

Edit 编辑

When I tried the .load function I still ran into issues with IE below is the commented code. 当我尝试.load函数时,我仍然遇到IE问题,下面是注​​释的代码。 I have numerous alerts to try and figure out what the issue is. 我有很多警报要尝试找出问题所在。 It seems #2 should work but in IE returns null. 似乎#2应该可以工作,但是在IE中返回null。

Is it possible to manipulate the [object Object] or to see what it contains? 是否可以操纵[object Object]或查看其包含的内容?

var divItems = $("#tab-bbp a").attr("href");
 $("#persistenslcont").load(slItems + " #persistenslcont",
                                    function (data, textStatus, req) {
                                        //returns HTML code in the alert in IE & FF
                                        alert("test 1" + req.responseText);

                                        //returns null in IE - In FF returns proper code
                                        alert("test 2" + $("#persistenslcont",req.responseText).html());

                                        //returns [object Object] same in IE & FF
                                        alert("test 3" + $("#persistenslcont",data));

                                        //returns [object Object] same in IE & FF
                                        var $response = $('<div />').html(data);
                                        var $test4 = $response.find('#persistenslcont');
                                        $('#persistenslcont').append($test4);
                                        alert("test 4" + $test4);

                                        if (textStatus == "error") {
                                          alert("error");
                                        }
                                  });

i think you can make the success function simpler like this: 我认为您可以使成功功能更简单:

success: function(msg){
   $("#persistenslcont").html(msg);

   alert($("#persistenslcont").html(msg));

} }

You can specify what part of a requested page you want in jquery. 您可以在jquery中指定所需页面的哪一部分。 I would use the load() function: 我将使用load()函数:

     $("#persistenslcont").load($(divItems + " #divcontent");

I think I botched your code, but the basic idea is right and should work in IE. 我想我把您的代码搞砸了,但是基本思想是正确的,应该可以在IE中使用。 Basically just point the ajax query to the part of the page you want, rather then dealing with messing around inside the HTML of the entire page requested. 基本上,只是将ajax查询指向所需的页面部分,而不是处理整个请求页面的HTML内容。

The problem with my Ajax call in IE was related to an open <div> . 我在IE中调用Ajax的问题​​与打开的<div> Somehow I didn't check my HTML structure at first. 不知何故,我一开始没有检查我的HTML结构。 It's just another reason why HTML validation is important! 这只是HTML验证很重要的另一个原因!

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

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