I have a simple $.ajax
call that returns a response that looks like this:
<div id='type'>success</div>
<div id="graph"></div>
<script type='text/javascript'>
//some script that manipulates the graph div
</script>
and on my .ajax's success(response) function, I have:
$(response).find('#type').text()开发者_StackOverflow
my problem is that it always returns null if I use .html()
and 'an empty string' when i use .text();
Is there something wrong with my syntax?
It's because it's at the root level of the response, so .find()
won't work (because it's not a descendant), you need .filter()
, like this:
$(response).filter('#type').text()
Your response HTML needs to have a root element so that it can find()
elements, because find()
searches descendants of a DOM element. You can achieve this by adding the response to the document's DOM, or to another jQuery DOM element.
You can also do this and it'll work as well:
$('<div>' + response + '</div>').find('#type').text();
here you can test your code should work fine. you have to have a problem in ajax, try with "alert" to "response" to see what response you get
test
It cant be something as simple as the single quotes around the id in the type div can it?
(the html, not the js)
In your $.ajax()
request, set dataType
as html
精彩评论