ResponseXML - null

url = "http://localhost/xml.php?type=xml"; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); xmlhttp.open("GET", url, true); xmlhttp.setRequestHeader('Content-Type', 'application/xml'); xmlhttp.send(null); } else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); if (xmlhttp) { xmlhttp.open("GET", url, true); xmlhttp.setRequestHeader('Content-Type', 'application/xml'); xmlhttp.send(); } } alert(xmlhttp.responseXML); //returns null 

Xml file

 <?xml version="1.0" encoding="UTF-8" ?> <main> <food> <type>6</type> <region>5676</region> </food> <food> <type>6</type> <region>5676</region> </food> </main> 

Anyone have an idea why xmlhttp.responseXML returned as null?

+2
source share
4 answers

Your HTTP request is asynchronous. xmlhttp.responseXML will not matter until xmlhttp.readyState is 4 .

 var url = "http://localhost/xml.php?type=xml"; var xmlhttp; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp) { xmlhttp.open("GET", url, true); xmlhttp.setRequestHeader('Content-Type', 'text/xml'); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4) { alert(xmlhttp.responseXML); } }; xmlhttp.send(); } 

Additionally, I don't think you need a setRequestHeader string. The response requires an XML MIME type, not a request. Also, please observe good coding rules (do not forget var , DRY, etc.)

+4
source

Make sure you have header('Content-type: application/xml'); in your PHP script. Also check out answerText - maybe you have a bug?

+3
source

I recently switched from Apache to nginx and had the same problem. Everything worked fine when downloading as simple files or from an Apache server, but responseXML always empty when running on nginx.

In my specific situation, an XSL stylesheet was also used to transform the XML:

 <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="main-template-transformer.xsl"?> 

The content type of a regular XML file was returned just fine. However, the content type of the XSL file was important. (This was discovered by checking the responseText , which was not null, and contained the entire text of the XSL file. Checking the HTTP headers in this file showed that the content type was changed between Apache and nginx.)

The content type must be either text/xml or application/xml . The default value in nginx 1.10.3 is application/octet-stream , and this will cause responseXML to always be zero.

This can be fixed by adding the following line to the JavaScript file:

 xmlhttp.overrideMimeType('text/xml'); 

This can be fixed by adding the following line to the nginx server configuration in "conf / mime.types":

  text/xml xsl; 
+1
source

I just checked and found a solution.

I don't know why, but when you send the xml header, XMLHttpRequest is not able to parse it.

To use the responseXML XMLHttpRequest DOM properties, you need to remove the xml header.

In your case, the xml response will be

 <main> <food> <type>6</type> <region>5676</region> </food> <food> <type>6</type> <region>5676</region> </food> </main> 
0
source

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


All Articles