Get specific content without response

I am making an ajax call and I am getting the content from the responseText .

I confirm that inside the alert inside the responseText contains the whole page as a string .

It's good. Now I need to extract the specific content from the string, translating it to the DOM and using getElementsByTagName , getElementsByName , etc.

My problem is that none of them work.

I have read many references to using responseXML instead of responseText , but this returns me null .

So, adhering to responseText , how can I get the specific elements that I want?

For example, if my answer contains the following:

 <html> <head>....</head> <body>.... ..... <table> <tr> <td>sometext</td> <td>someothertext</td> </tr> <tr> <td>sometext</td> <td>someothertext</td> </tr> <tr> <td>sometext</td> <td>someothertext</td> </tr> </table> <div> somediv </div> </body> </html> 

How can I access the second row of the table and its value? Or div, etc. so now in my actual html, I have sth like this

 <html> <head>.....</head> <body> <table><tr><td id="test">Test</td></tr></table> </body> </html> 

I want the extracted item / value to be parsed inside the table of my actual html ..

document.getElementById ('test'). innerHTML = the_extracted_elements_from_responseText

+4
source share
1 answer

You need to use DOMParser in html and use the DOM traversal methods in the resulting document to return the desired node (s).

  var parser=new DOMParser(); var xmlDoc=parser.parseFromString(responseText,"text/xml"); var tds = xmlDoc.getElementsByTagName("td"); 

Note that IE will require new ActiveXObject("Microsoft.XMLDOM"); instead new ActiveXObject("Microsoft.XMLDOM"); .

+10
source

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


All Articles