1

我试图获取一个 XML,然后 response.write 到一个页面(在我的服务器上),这样我以后可以通过 Ajax 请求(javascript)得到它。但是当我尝试这个时,文档以 HTML 页面的形式出现带有 XML 的节点:http: //imgur.com/GL47U

如果我用浏览器访问 url,它会显示正确的 XML,所以我猜它的源代码没有错误?

这是 page_load 上调用的代码:

public void getXML(){

            WebRequest req = WebRequest.Create("url");
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            req.ContentType= "text/xml charset=utf8";

            Stream streamdata = resp.GetResponseStream();
            StreamReader reader = new StreamReader(streamdata);

            string serverresponse = reader.ReadToEnd();

            reader.Close();
            streamdata.Close();
            resp.Close();

            Response.Write(serverresponse);
        }

我错过了什么?(是的,我是新手!) tnx

javascript: 变量 xmlhttp;

        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }

          xmlhttp.onreadystatechange=function()
          {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                console.log(xmlhttp.responseXML);
            }
          }

        xmlhttp.open("GET", "http://127.0.0.1:8080/api.aspx?METHOD=getXML",true);
        xmlhttp.setRequestHeader("Content-type", "application/xml");
        xmlhttp.send();
4

2 回答 2

2

HTML (api.aspx)

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" ContentType="text/xml" %>

代码隐藏 (api.aspx)

public partial class api: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        getXML();
    }


    public void getXML()
    {

        WebRequest req = WebRequest.Create("http://webdev.clic.det.nsw.edu.au/Mum12/Public/Sample.xml");
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        req.ContentType = "text/xml charset=utf8";

        Stream streamdata = resp.GetResponseStream();
        StreamReader reader = new StreamReader(streamdata);

        string serverresponse = reader.ReadToEnd();

        reader.Close();
        streamdata.Close();
        resp.Close();

        Response.Write(serverresponse);
    }
}

这就是我在 test.aspx 中使用它的方式

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
</head>
<body>
    <script>
        $(document).ready(function () {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    console.log(xmlhttp.responseXML);
                }
            };

            xmlhttp.open("GET", "http://localhost/testwebsite/api.aspx", true);
            xmlhttp.send();
        });



    </script>
</body>
</html>

我得到了预期的xml。请测试并让我知道它是否有帮助。

于 2012-10-17T23:42:23.710 回答
1

您需要设置响应的内容类型,以便浏览器正确处理它:

Response.ContentType = "text/xml";

正如 Tariqulazam 所说,页面内容可能还可以。要查看实际发生的情况,请使用“查看页面源代码”而不是在开发工具中查看。

于 2012-10-17T23:00:50.490 回答