Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing returned HTML from jQuery AJAX request

What I'm trying to do seems simple: get an HTML page through $.ajax() and pull out a value from it.

$(function () {
    $.ajax({
        url: "/echo/html",
        dataType: "html",
        success: function (data) {
            $('#data').text(data);
            $('#wtf').html($(data).find('#link').text());
        },
        data: {
            html: '<!DOCTYPE html><head><title><\/title><link href="../css/popup.css" rel="stylesheet" /><\/head><body><ul><li><a id="link">content<\/a><\/li><\/ul><\/body><\/html>'
        }
    });
});

The problem is that jQuery refuses to parse the returned HTML.

The fiddle I'm play with this in isn't working in the mean time, so there's little else I can do to provide a working example.

UPDATE: My new fiddle is working fine, but it seems the problem is that in my actual project I'm trying to parse a large, complex bit of HTML. Is this a known problem?

like image 568
Stephen Collins Avatar asked Nov 15 '13 18:11

Stephen Collins


Video Answer


7 Answers

Your code works fine. You just aren't using jsFiddle's API correctly. Check the docs for /echo/html/ (http://doc.jsfiddle.net/use/echo.html#html):

URL: /echo/html/

Data has to be provided via POST

So, you need to update your AJAX call to use POST. Also the trailing slash is needed.

$(function () {     $.ajax({         url: "/echo/html/",         type: "post",         dataType: "html",         success: function (data) {             $('#data').text(data);             $('#wtf').html($(data).find('#link').text());         },         data: {             html: '<!DOCTYPE html><head><title><\/title><link href="../css/popup.css" rel="stylesheet" /><\/head><body><ul><li><a id="link">content<\/a><\/li><\/ul><\/body><\/html>'         }     }); }); 

DEMO: http://jsfiddle.net/hcrM8/6/

like image 51
Rocket Hazmat Avatar answered Sep 18 '22 16:09

Rocket Hazmat


If you would like to parse it, jquery has a nifty trick :)

 ParsedElements = $(htmlToParse);
 Console.log(ParsedElements);

You now have DOM elements you can traverse without placing them in the body of the document.

like image 20
Anthony Iacono Avatar answered Sep 20 '22 16:09

Anthony Iacono


jQuery.parseHTML()

http://api.jquery.com/jQuery.parseHTML/

str = "hello, <b>my name is</b> jQuery.",
  html = $.parseHTML( str ),
  nodeNames = [];

// Gather the parsed HTML's node names
$.each( html, function( i, el ) {
  nodeNames[ i ] = "<li>" + el.nodeName + "</li>";
});

Some thing is wrong with your ajax on fiddle

http://jsfiddle.net/hcrM8/5/

var html= '<!DOCTYPE html><head><title><\/title><link href="../css/popup.css" rel="stylesheet" /><\/head><body><ul><li><a class="disabled" id="link">content<\/a><\/li><\/ul><\/body><\/html>';
            h = $.parseHTML(html);
            $('#data').text(h);
            $('#wtf').html($(h).find('#link').text());
like image 41
Rizwan Mumtaz Avatar answered Sep 22 '22 16:09

Rizwan Mumtaz


Why don't you just use the load method?

$( "#wtf" ).load( "/echo/html #link" );

Or, here's your fiddle fixed and working:

http://jsfiddle.net/hcrM8/4/

like image 32
dave Avatar answered Sep 22 '22 16:09

dave


I had the same problem and i fixed encapsulating requested html code into just one container element.

Bad Example:

<a href="link">Linkname</a>
<p>Hello world</p>

Jquery couldnt convert this to element, because it wishes to convert a single element tree. But those are not having a container. Following example should work:

Right Example:

<div>
    <a href="link">Linkname</a>
    <p>Hello world</p>
</div>
like image 33
wmwmwm Avatar answered Sep 22 '22 16:09

wmwmwm


I am facing the same problem, and it is not because you are doing something wrong.

it's because the "link" tag is not supposed to have any innerHTML returned, it's explicitly excluded in jquery, you will find some where this line:

rnoInnerhtml = /<(?:script|style|link)/i,

This tag in HTML is supposed to link to external style sheet.

like image 34
ZMs Avatar answered Sep 22 '22 16:09

ZMs


All answers do not point to the real problem, jQuery seems to ignore the head and body tag and creates an array of nodes. What you normally want is, extract the body and parse it.

Take a look at this helpful answer, I do not want to copy his work: https://stackoverflow.com/a/12848798/2590616.

like image 23
RiZKiT Avatar answered Sep 20 '22 16:09

RiZKiT