When making an Ajax request, the response I get is an XML like the following:
<wp_ajax>
<response action="add-tag_0">
<taxonomy id="0" position="1">
<response_data/>
<supplemental>
<parents></parents>
<noparents/>
</supplemental>
</taxonomy>
</response>
<response action="add-tag_0">
<term id="0" position="0">
<response_data/>
<supplemental>
<term_id>514</term_id>
<name>Juan Lopez</name>
<slug>juan-lopez</slug>
<term_group>0</term_group>
<term_taxonomy_id>514</term_taxonomy_id>
<taxonomy>booked_custom_calendars</taxonomy>
<description><p>[email protected]</p></description>
<parent>0</parent>
<count>0</count>
<filter>display</filter>
</supplemental>
</term>
</response>
</wp_ajax>
I run it as follows:
jQuery(xml).find('response').each(function(i) {
var term_id = ((xml.getElementsByTagName('term_id')[i]).childNodes[0]).nodeValue;
jQuery('#esb_subfield_cus_field_calendario').val('[booked-calendar calendar='+ term_id +']');
});
But it throws me the following error:Cannot read property 'childNodes' of undefined
Of so many ways that I found to go through the XML, this was the one that worked for me but it throws me the error and all the JS that follows does not execute. It should be clarified that the Script is executed and performs the function but, as I say, it returns the error and all the JS that follows is not executed.
Your error is a simple validation problem.
You are iterating the number of times that the element exists
response
, that is, in the XML of your example you would iterate twice having the indexes0
and1
, but when trying to capture the elementterm_id
in the index0
said element does not exist, that is why it takes the mistake.The solution is simple, before capturing the value of the element
term_id
you must validate that it actually exists.In the example I made a
parseo
so it could work, ignore those settings.