【问题标题】:responseXML from XMLHttpRequest Object doesn't work来自 XMLHttpRequest 对象的 responseXML 不起作用
【发布时间】:2014-10-14 02:43:58
【问题描述】:

拜托,谁能帮助我,让我知道我做错了什么。我正在使用 ASP.NET 动态返回一个 XML 文件。这是我在 ASP.NET 中的 VB 代码

<%@ Page Language="VB" ContentType="text/xml"%>   
<%@ Import namespace="System.Xml"%>   
<%@ Import namespace="System.Text"%>     


Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)   
    Dim writer As XmlTextWriter
        writer = New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
        writer.WriteStartDocument()
        writer.WriteStartElement("options")
        writer.WriteElementString("option", "Rojo")
        writer.WriteElementString("option", "Verde")
        writer.WriteEndElement()
        writer.WriteEndDocument()
        writer.Close()
End Sub

然后,我尝试使用 AJAX 获取带有 responseXML 的 XML 文件,但没有任何反应。 responseXML 不检索 XML。任何帮助将不胜感激。我使用警报来查看代码是否从 responseXML 返回了某些内容。 responseXML 返回 null。

这是我使用 responseXML 的 javacript 代码。谢谢。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Using Ajax and XML</title>

<script language="javascript">

    var XMLHttpRequestObject = false;
    //var xmlDocument;
    var options;

    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
        /****Indicates the Firefox browser that the returned data will have a content type/
        if (XMLHttpRequestObject.overrideMimeType) {
            XMLHttpRequestObject.overrideMimeType("text/xml");
        }
        /******************************************************/
    } else if (window.ActiveXObject) {
            XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }

        function getoptions1()
        {
            if (XMLHttpRequestObject)
            {
                XMLHttpRequestObject.open("GET", "options1.aspx", true);
                //XMLHttpRequestObject.setRequestHeader("Content-Type", "text/xml");

                XMLHttpRequestObject.onreadystatechange=function()
                {
                  if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
                   {               
                       var xmlDocument = XMLHttpRequestObject.responseXML;
                       alert(xmlDocument);
                       options = xmlDocument.documentElement.getElementsByTagName("option");
                       alert(options.length);  
                }
            }
            XMLHttpRequestObject.send(null);
        }
    }


</script>
</head>
<body>
    <h1>Using Ajax and XML</h1>
    <form>
        <select size="1" id="optionList" onchange="setoption()">
            <option>Select a scheme</option>
           <option>Select a scheme</option>
        </select>

        <input type="button" value="Use color scheme 1" onclick="getoptions1()">
        <input type="button" value="Use color scheme 2" onclick="getoptions2()">

</form>

<div id="targetDiv">Set the color of this text</div>

</body>
</html>

【问题讨论】:

    标签: asp.net xml ajaxform


    【解决方案1】:

    我发现了问题所在。我没有使用 aspx 文件来输出我的XML,而是使用了 ashx 文件。这是创建的 ashx 文件

    <%@ WebHandler Language="VB" Class="options1" %>
    Imports System
    Imports System.Web
    Imports System.Xml
    
    Public Class options1 : Implements IHttpHandler
    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest      
        Dim doc As XmlDocument
        Dim str As String = ""
        Dim options() As String = {"red", "green", "blue"}
        Dim arrayItem As String
    
        For Each arrayItem In options
            str = str + "<option>" + arrayItem + "</option>"
        Next
        doc = New XmlDocument
        doc.LoadXml("<options>" + str + "</options>") '("<options><option>red</option></options>")
        context.Response.ContentType = "text/xml"
        context.Response.ContentEncoding = Encoding.UTF8
        context.Response.Cache.SetAllowResponseInBrowserHistory(True)
        doc.Save(context.Response.Output)
    End Sub
    

    然后在javascript中我在XMLHttpRequest的open方法中调用了ashx文件。这是代码。

    <script language="javascript">
        var XMLHttpRequestObject = false;
        var xmlDocument;
        var options;
    
        if (window.XMLHttpRequest) {
            XMLHttpRequestObject = new XMLHttpRequest();            
            if (XMLHttpRequestObject.overrideMimeType) {
                XMLHttpRequestObject.overrideMimeType("text/xml");
            }
        } else if (window.ActiveXObject) {
            XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        function getoptions1()
        {
            if (XMLHttpRequestObject)
            {
                XMLHttpRequestObject.open("GET", "options1.ashx", true);  
                XMLHttpRequestObject.onreadystatechange=function()
                {
                   if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
                   {
                       xmlDocument = XMLHttpRequestObject.responseXML;
                       options = xmlDocument.getElementsByTagName("option");
                       alert(options.length);
                    }
                }
                XMLHttpRequestObject.send(null);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 2012-07-18
      相关资源
      最近更新 更多