How to parse jquery ajax xhtml answer?

Sorry if this has been published many times. But I tried many options and it still does not work. HTML is coming back from jQuery AJAX, and I am trying to remove the header and footer from the response using:

// none of these work for me $("#content", data); $("#content", $(data)); $(data).find("#content").html() 

I left a response to verify that #content exists by checking $ (data) and using the warning to print the text of the data. I also try to use "body" or "a" as selectors, but always returns as undefined.

I read in this post that you cannot get the full XHTML document: jquery ajax parse response text . But I can no longer find the answer quote, maybe it is out of date?

Has anyone encountered this problem?

Thanks a lot Steve

+4
source share
6 answers

this works for me:

 $(data).filter("#content"); 
+4
source

You need a div to attach your data. Like $("#response").replaceWith($(data).find('#content')); This should work

+2
source

It works if there is a <div> tag in the received document.

eg. <body><div> your content </div></body> .

See a very simple proof of concept .

+2
source

The data received from your AJAX call is not part of your DOM tree, so you cannot use jQuery function calls to manage it. You can use text manipulation functions, you can use JSON, or you can also attach your answer to your DOM.

+1
source

Do you use get method? Alternatively, you can use the jQuery download method, where you can provide a downloadable snippet of the page.

For example, to load the contents of a div from a well-formed html document, you can use

$ ("# div-to-load-to"). load ("html-doc-to-load-from.html #content", function () {// do something});

0
source

You MUST ensure that your HTML home has been well formatted.

Try the simplest HTML

 <html> <head> <title>title here</title> </head> <body> <div> <div>hello</div> <span id="s">span content</span> </div> </body> 

0
source

Source: https://habr.com/ru/post/1306856/


All Articles