开发者

responseXML is 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 has idea why xmlhttp.responseXML is returning as null?


Your HTTP request is asynchronous. xmlhttp.responseXML won't have some value until xmlhttp.readyState has the value of 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();
}

Additionaly, I don't think you need the setRequestHeader line. XML MIME type is required for response, not for request. Also, please respect good coding practices (don't forget var, DRY, etc.)


Make sure you have header('Content-type: application/xml'); in your PHP script. Additionally, check the responseText - maybe you've got error?


Recently, I migrated from Apache to nginx and had this same problem. Everything worked perfectly when loaded as simple files or from the Apache server, but responseXML was always null when running on nginx.

My particular situation also involved an XSL stylesheet 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 the regular XML file was coming back just fine. However, what mattered was the content type of the XSL file. (This was discovered by checking responseText which was not null, and contained the entire text of the XSL file. Checking HTTP headers on this file revealed that the content type had changed between Apache and nginx.)

Content type should be either text/xml or application/xml. The default in nginx 1.10.3 is application/octet-stream, and this will cause responseXML to be always null.

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;


I've just testing and found the solution.

I don't kwow why but when you send xml header the XMLHttpRequest is not capable to parse it.

For using DOM responseXML properties of XMLHttpRequest you have 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜