C# 响应。写入webrequest不';无法正确显示XML

C# 响应。写入webrequest不';无法正确显示XML,c#,asp.net,ajax,C#,Asp.net,Ajax,我尝试获取一个XML,然后进行响应。将其写入页面(在我的服务器上),以便稍后通过Ajax请求(javascript)获取它。。但当我尝试此操作时,文档显示为一个HTML页面,其中有一个包含XML的节点: 如果我在浏览器中访问url,它会显示正确的XML,所以我猜它的源代码没有错误 下面是在加载页面上调用的代码: public void getXML(){ WebRequest req = WebRequest.Create("url"); Ht

我尝试获取一个XML,然后进行响应。将其写入页面(在我的服务器上),以便稍后通过Ajax请求(javascript)获取它。。但当我尝试此操作时,文档显示为一个HTML页面,其中有一个包含XML的节点:

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

下面是在加载页面上调用的代码:

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: 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://127.0.0.1:8080/api.aspx?METHOD=getXML",true);
        xmlhttp.setRequestHeader("Content-type", "application/xml");
        xmlhttp.send();

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

Response.ContentType = "text/xml";
正如塔里克拉扎姆所说,页面内容可能还可以。要了解实际情况,请使用“查看页面源代码”而不是在开发工具中查看它。

HTML(api.aspx)

这就是我在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>

$(文档).ready(函数(){
var-xmlhttp;
if(window.XMLHttpRequest){//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
log(xmlhttp.responseXML);
}
};
open(“GET”http://localhost/testwebsite/api.aspx“,对);
xmlhttp.send();
});

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

哦,我使用asp.net,上面的代码在codebehind中。aspx页面只有这个,我认为那里一切都很好。这就是chrome处理XML文件的方式。你试过FireFox吗?是的,相同:/i当我使用ajax控制台.log(xmlhttp.responseText)时,字符串看起来不错,但是当使用xmlhttp.responseXML时,我得到了空值。请发布你的javascript代码。正如Kevin提到的,在.aspx页面指令中添加ContentType=“text/xml”,应该可以。我已经在上面设置了.ContentType(第三行)?如果我在页面源代码中查看它,它看起来就像我想要的一样!:)但是,当我尝试使用Ajax请求获取它时,xmlhttp.responseXML将为null,但xmlhttp.responseText看起来像我想要的xml:(有什么想法吗?在我看来,您是在设置请求的内容类型,而不是响应。我想您应该设置resp.ContentType而不是req.ContentType。简单地说,您可以尝试将内容类型设置为“text/xml;charset=utf-8”或“text/xml”。可能不会有什么区别,因为错误表明服务器端存在问题。它可以工作!是api.aspx中的ContentType=“text/xml”完成的!天哪!你是我的英雄!非常感谢你!)我将给Kevin的解决方案+1,因为从技术上讲,这与他提到的是同一件事,我很高兴它起了作用。谢谢Tariqlazam。在页面指令上打了一个很好的电话。
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);
    }
}
<%@ 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>