无法从ajax响应中获取body元素

时间:2022-04-27 09:48:44

The response is a HTML document (from a request from a link's href)

响应是HTML文档(来自链接href的请求)

 var data = $(response).find('body').html();
 alert(data);  

 // I get a alert with nothing...

full code:

完整代码:

     $.ajax({
       url: $(this).attr('href'),
       type: 'GET',      
       success: function(response){
         var data = $(response).find('body').html();
         alert(data);  
        }
     });

3 个解决方案

#1


5  

Use this tricky code ;)

使用这个棘手的代码;)

   /*this will get the body content and head by replacing them with div before placing them inside the jQuery factory witch will avoid all bugs, i used this while creating the ajaxit jquery plugin :) */
$.ajax({
                type: "GET",
                url: $(this).attr('href'),
                async:true,
                error:function (event, request, options, error) {
                    if (ajaxItMain.onError){
                        ajaxItMain.onError(event,request,options,error);
                    }
                },
                success:  function (data) {
                    // ----------------- < data >
                    // clearing CDATA
                    data=data.replace(/\<\!\[CDATA\[\/\/\>\<\!\-\-/gi,'');
                    data=data.replace(/\/\/\-\-\>\<\!\]\]\>/gi,'');

                    // extracting the the head and body tags
                    var dataHead = data.match(/<\s*head.*>[\s\S]*<\s*\/head\s*>/ig).join("");
                    var dataBody = data.match(/<\s*body.*>[\s\S]*<\s*\/body\s*>/ig).join("");
                    var dataTitle = data.match(/<\s*title.*>[\s\S]*<\s*\/title\s*>/ig).join("");

                    dataHead  = dataHead.replace(/<\s*head/gi,"<div");
                    dataHead  = dataHead.replace(/<\s*\/head/gi,"</div");

                    dataBody  = dataBody.replace(/<\s*body/gi,"<div");
                    dataBody  = dataBody.replace(/<\s*\/body/gi,"</div");

                    dataTitle = dataTitle.replace(/<\s*title/gi,"<div");
                    dataTitle = dataTitle.replace(/<\s*\/title/gi,"</div");


                    // comments
                    var commentPattern = /\<\!\-\-([\s\S]*?)\-\-\>/ig;

                    // get head comment tags
                    var headComments = dataHead.match(commentPattern);

                    // get body comment tags
                    var bodyComments = dataBody.match(commentPattern);

                    // head - body - title content
                    var $dataHead    = $(dataHead);
                    var $dataTitle   = $(dataTitle);
                    var $dataBody    = $(dataBody);
                }
            });
        }

#2


9  

Try it this way:

试试这种方式:

var $dom = $(document.createElement("html"));
$dom[0].innerHTML = response;

var $body = $dom.find("body");

#3


1  

Passing string which represents an entire HTML document into the jQuery factory results in a collection containing many of the tags from within the string, not a single element representing a total page.

将表示整个HTML文档的字符串传递到jQuery工厂会产生一个集合,其中包含字符串中的许多标记,而不是表示总页面的单个元素。

You need to create a DOM document out of the markup, and pass that into jQuery.

您需要从标记中创建一个DOM文档,并将其传递给jQuery。

As a starter, you could try telling $.ajax that you're expecting back XML so that it is parsed into a DOM Document for you. However, the resulting DOM document would not allow for methods such as .html() to be called against it as it would not be considered HTML.

作为入门者,您可以尝试告诉$ .ajax您期望返回XML,以便将其解析为DOM文档。但是,生成的DOM文档不允许调用.html()等方法,因为它不会被视为HTML。

#1


5  

Use this tricky code ;)

使用这个棘手的代码;)

   /*this will get the body content and head by replacing them with div before placing them inside the jQuery factory witch will avoid all bugs, i used this while creating the ajaxit jquery plugin :) */
$.ajax({
                type: "GET",
                url: $(this).attr('href'),
                async:true,
                error:function (event, request, options, error) {
                    if (ajaxItMain.onError){
                        ajaxItMain.onError(event,request,options,error);
                    }
                },
                success:  function (data) {
                    // ----------------- < data >
                    // clearing CDATA
                    data=data.replace(/\<\!\[CDATA\[\/\/\>\<\!\-\-/gi,'');
                    data=data.replace(/\/\/\-\-\>\<\!\]\]\>/gi,'');

                    // extracting the the head and body tags
                    var dataHead = data.match(/<\s*head.*>[\s\S]*<\s*\/head\s*>/ig).join("");
                    var dataBody = data.match(/<\s*body.*>[\s\S]*<\s*\/body\s*>/ig).join("");
                    var dataTitle = data.match(/<\s*title.*>[\s\S]*<\s*\/title\s*>/ig).join("");

                    dataHead  = dataHead.replace(/<\s*head/gi,"<div");
                    dataHead  = dataHead.replace(/<\s*\/head/gi,"</div");

                    dataBody  = dataBody.replace(/<\s*body/gi,"<div");
                    dataBody  = dataBody.replace(/<\s*\/body/gi,"</div");

                    dataTitle = dataTitle.replace(/<\s*title/gi,"<div");
                    dataTitle = dataTitle.replace(/<\s*\/title/gi,"</div");


                    // comments
                    var commentPattern = /\<\!\-\-([\s\S]*?)\-\-\>/ig;

                    // get head comment tags
                    var headComments = dataHead.match(commentPattern);

                    // get body comment tags
                    var bodyComments = dataBody.match(commentPattern);

                    // head - body - title content
                    var $dataHead    = $(dataHead);
                    var $dataTitle   = $(dataTitle);
                    var $dataBody    = $(dataBody);
                }
            });
        }

#2


9  

Try it this way:

试试这种方式:

var $dom = $(document.createElement("html"));
$dom[0].innerHTML = response;

var $body = $dom.find("body");

#3


1  

Passing string which represents an entire HTML document into the jQuery factory results in a collection containing many of the tags from within the string, not a single element representing a total page.

将表示整个HTML文档的字符串传递到jQuery工厂会产生一个集合,其中包含字符串中的许多标记,而不是表示总页面的单个元素。

You need to create a DOM document out of the markup, and pass that into jQuery.

您需要从标记中创建一个DOM文档,并将其传递给jQuery。

As a starter, you could try telling $.ajax that you're expecting back XML so that it is parsed into a DOM Document for you. However, the resulting DOM document would not allow for methods such as .html() to be called against it as it would not be considered HTML.

作为入门者,您可以尝试告诉$ .ajax您期望返回XML,以便将其解析为DOM文档。但是,生成的DOM文档不允许调用.html()等方法,因为它不会被视为HTML。