JS XMLHttpRequest()Connect.open适用于文件夹中的文件,但不适用于http

时间:2018-09-17 18:07:58

标签: javascript http

我正在尝试从HTTP站点加载XML文件(将位于同一网络中),所有文件都可以正常加载到同一文件夹中(如代码部分Connect.open(“ GET”,“ REPORT.XML “,false);)我同时尝试了true和false,但仍然无法正常工作。我想念什么?如下面的代码在尝试加载除REPORT.XML以外的任何内容(未使用http版本)时将显示所有变量的undefined。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Consommation</title>

<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=Generator content="Microsoft Word 15 (filtered)">
<style>
    <!--
    /* Font Definitions */
    @font-face
    {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
    @font-face
    {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
    /* Style Definitions */
    p.MsoNormal, li.MsoNormal, div.MsoNormal
    {margin-top:0in;
        margin-right:0in;
        margin-bottom:10.0pt;
        margin-left:0in;
        line-height:115%;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
    .MsoChpDefault
    {font-family:"Calibri",sans-serif;}
    .MsoPapDefault
    {margin-bottom:10.0pt;
        line-height:115%;}
    @page WordSection1
    {size:11.0in 8.5in;
        margin:42.55pt 1.0in 35.45pt 1.0in;}
    div.WordSection1
    {page:WordSection1;}
    -->
</style>

<script language="JavaScript">
    // Create a connection to the file.
    var Connect = new XMLHttpRequest();
    // Define which file to open and
    // send the request.
    //Connect.open("GET", "REPORT.XML", false); // THIS WORKS
    //Connect.open("GET", "http://192.168.40.15/REPORT.XML", false); // THIS DOES NOT WORK
    Connect.open("GET", "http://127.0.0.1/REPORT.XML", false); // THIS DOES NOT WORK
    Connect.setRequestHeader("Content-Type", "text/xml");
    Connect.send(null);
    // Place the response in an XML document.
    var TheDocument = Connect.responseXML;
    // Place the root node in an element.
    var Data = TheDocument.childNodes[0];


    //document.write(Data.getElementsByTagName("Name")[2].textContent + " of value of " + Data.getElementsByTagName("ActiveValue")[2].textContent + " " + Data.getElementsByTagName("ActiveUnit")[2].textContent + "<br>");
    //document.write(Data.getElementsByTagName("Name")[6].textContent + " of value of " + Data.getElementsByTagName("ActiveValue")[6].textContent + " " + Data.getElementsByTagName("ActiveUnit")[6].textContent + "<br>");
    //document.write(Data.getElementsByTagName("Name")[10].textContent + " of value of " + Data.getElementsByTagName("ActiveValue")[10].textContent + " " + Data.getElementsByTagName("ActiveUnit")[10].textContent + "<br>");

    var insta = Data.getElementsByTagName("ActiveValue")[2].textContent;
    var jourcour = Data.getElementsByTagName("ActiveValue")[6].textContent;
    var totalvieille = Data.getElementsByTagName("ActiveValue")[10].textContent;


</script>
</head>
<body>

<span lang=FR style='font-size:18.0pt;line-height:115%;color:red'><script language="JavaScript">document.write(insta);</script></span>
<span lang=FR style='font-size:18.0pt;line-height:115%;color:red'><script language="JavaScript">document.write(jourcour);</script></span>
<span lang=FR style='font-size:18.0pt;line-height:115%;color:red'><script language="JavaScript">document.write(totalvieille);</script></span>

</body>

0 个答案:

没有答案