Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

javascript - jquery ajax parse response text

Ok this is really frusturating me because I've done this a hundred times before, and this time it isn't working. So I know I'm doing something wrong, I just can't figure it out.

I am using the jQuery .get routine to load html from another file. I don't want to use .load() because it always replaces the children of the element I'm loading content into.

Here is my .get request:

$(document).ready(function() {
    $.get('info.html', {}, function(html) {
        // debug code
        console.log($(html).find('ul').html());
        // end debug code
    });
});

The file 'info.html' is a standard xhtml file with a proper doctype, and the only thing in the body is a series of ul's that I need to access. For some reason, the find function is giving me a null value.

In firebug, the GET request is showing the proper RESPONSE text and when I run

console.log(html);

Instead of the current console.log line, I get the whole info.html as output, like I would expect.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot pull in an entire XHTML document. You can only handle tags that exist within the <body> of an html document. Frustrating. Strip everything from info.html that isn't within your <body> tag and try it again.

There are other potential ways around this issue - check below "Stackoverflow Related Items" at the base of this response.

From the Doc: (http://docs.jquery.com/Core/jQuery#htmlownerDocument)

"HTML string cannot contain elements that are invalid within a div, such as html, head, body, or title elements."

Stackoverflow Related Items:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...