Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting html returned from ajax request into an object causes weird complications

I am calling an Ajax request with jQuery:

$.get(url, function(data){
    $(data).find('ul');
});

And here is a problem - it does not find any ul.

Returned HTML code looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pl">
<head>
    <title>Title</title>
    <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
    <meta name="description" content="Mobilna wersja serwisu GoldenLine." />
    <meta name="keywords" content="praca, networking, forum, blog, bran?a" />
    <link rel="stylesheet" type="text/css" href="http://css.goldenline.pl/mobile.css" />
    <link rel="shortcut icon" href="http://x/favicon.ico" />
</head>
<body>
    <h1><a id="h" href="http://x/"><img src="http://x/img/mobile.gif" alt="GoldenLine" /></a></h1>
    <h2>Moje grupy</h2>
    <ul>
        <li>
            <img src="http://x/img/ico-post.png" alt="" />
            <a href="http://x/forum/estimating-software">estimating software</a>
        </li>
        <li>
            <img src="http://x/img/ico-post.png" alt="" />
            <a href="http://x/forum/google-hacking">Google hacking</a>
        </li>
        <li>
            <img src="http://x/img/ico-post.png" alt="" />
            <a href="http://x/forum/google-plus">Google Plus</a>
        </li>
        <li>
            <img src="http://x/img/ico-post.png" alt="" />
            <a href="http://x/forum/praca-dla-informatykow">IT – Praca dla osób z...</a>
        </li>
        <li>
            <img src="http://x/img/ico-post.png" alt="" />
            <a href="http://x/forum/jezyki-skryptowe">Języki Skryptowe</a>
        </li>
        <li>
            <img src="http://x/img/ico-post.png" alt="" />
            <a href="http://x/forum/netcamp">Netcamp</a>
        </li>
    </ul>
    <p class="setAsRead"><a href="http://x/fora/ozn">Ozn</a></p>
    <form method="get" action="http://x/szukaj">
        <input type="text" name="q" />
        <input type="submit" value="Szukaj" />
    </form>
    <ul>
        <li>1. <img src="http://x/img/msg_box_ico.gif" alt="" /> <a href="http://x/skrzynka" accesskey="1">Skrzynka [0]</a></li>
        <li>2. <a href="http://x/kontakty" accesskey="2">Kontakty</a></li>
        <li>3. <a href="http://x/forum" accesskey="3">Moje grupy</a></li>
        <li>4. <a href="http://x/wylogowanie" accesskey="4">Wyloguj</a></li>
    </ul>
    <div class="footer">
        <p>2010 &copy; Goldenline.pl | <a href="http://x/www">przejdź do normalnej wersji serwisu</a></p>
    </div>
</body>
</html>

But when I convert it into an object

$(data)

I got something like this:

[
<!--?xml version="1.0" encoding="UTF-8"?-->, 
Text, 
<title>?GoldenLine.pl?</title>?, 
Text, 
<meta http-equiv=?"content-type" content=?"application/?xhtml+xml;? charset=utf-8">?, 
Text, 
<meta name=?"description" content=?"Mobilna wersja serwisu GoldenLine.">?, 
Text, 
<meta name=?"keywords" content=?"praca, networking, forum, blog, bran?a">?, 
Text, 
<link rel=?"stylesheet" type=?"text/?css" href=?"http:?/?/?css.goldenline.pl/?mobile.css">?, 
Text, 
<link rel=?"shortcut icon" href=?"http:?/?/?m.goldenline.pl/?favicon.ico">?, 
Text, 
<h1>?…?</h1>?, 
Text, 
<h2>?Moje grupy?</h2>?, 
Text, 
<ul>?…?</ul>?, 
Text, 
<p class=?"setAsRead">?…?</p>?, 
Text, 
<form method=?"get" action=?"http:?/?/?m.goldenline.pl/?szukaj">?…?</form>?, 
Text, 
<ul>?…?</ul>?, 
Text, 
<div class=?"footer">?…?</div>?, 
Text, 
<script type=?"text/?javascript">?…?</script>?, 
Text
]

What should I do to match <ul> elements in this piece of code returned from Ajax request ?

like image 492
hsz Avatar asked Aug 23 '11 09:08

hsz


2 Answers

i think you should first insert returned html data in one html element.

var mytag=$('<div></div>').html(data);

now using jQuery you can extract all ul nodes.

$('ul',mytag);

it will return array of all ul tags.

like image 51
vicky Avatar answered Sep 24 '22 13:09

vicky


Your return format is not html, looks rather like JSON to me. Try to enforce HTML by

$.get(url, function(data){
    $(data).find('ul');
}, "html");

Next thing I'd do is to see what the Content-Type of the server response is (using FireBug or something similar). It should say text/html there. If not, then the problem is probably on your back end, need to ensure that it sets the Content-Type correctly.

Tried it on my machine, if the Content-Type is OK, then it works like a charm, also without enforcing html in the jQuery.get.

like image 26
emboss Avatar answered Sep 25 '22 13:09

emboss