最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot get body element from ajax response - Stack Overflow

programmeradmin1浏览0评论

The response is a HTML document (from a request from a link's 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);  
        }
     });

The response is a HTML document (from a request from a link's 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);  
        }
     });
Share Improve this question asked Aug 9, 2011 at 19:37 BiberFiverBiberFiver 1671 gold badge2 silver badges5 bronze badges 6
  • Does the response contain body element? – ShankarSangoli Commented Aug 9, 2011 at 19:41
  • @BiberFiver: Does alert(response) output anything? – Evan Mulawski Commented Aug 9, 2011 at 19:42
  • yes, the entire html, yes it has the body in it, it's normal html page – BiberFiver Commented Aug 9, 2011 at 19:42
  • can you try to alert data to see what is the response, or if you are using firebug you can see it in response tab – Senad Meškin Commented Aug 9, 2011 at 19:43
  • first try to console.log($(response)) and check if it is one that you expect – tsds Commented Aug 9, 2011 at 19:44
 |  Show 1 more comment

3 Answers 3

Reset to default 12

Try it this way:

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

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

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);
                }
            });
        }

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.

You need to create a DOM document out of the markup, and pass that into 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.

发布评论

评论列表(0)

  1. 暂无评论