XMLHttpRequest对象的responseXML不起作用

时间:2014-08-20 16:25:15

标签: asp.net xml ajaxform

拜托,让我知道我做错了什么可以帮助我。我使用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>

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我没有使用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);
        }
    }