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 © 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 ?
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With