简体   繁体   English

Response.Write webrequest无法正确显示XML

[英]Response.Write webrequest doesn't display XML correctly

Im trying to get a XML and then response.write it to a page (on my server) so I can get it with an Ajax request (javascript) later.. But when I try this the document comes out as a HTML-page with a node with the XML: http://imgur.com/GL47U 我试图获取一个XML然后response.write它到一个页面(在我的服务器上)所以我可以在以后用Ajax请求(javascript)得到它..但是当我尝试这个时,该文档作为一个HTML页面出现XML节点: http//imgur.com/GL47U

If I go to the url in with my browser it displays the XML correct, so I guess its no erros with the source? 如果我使用浏览器访问url,它会显示正确的XML,所以我猜它与源代码没有错误?

Heres the code thats called on page_load: 下面是在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);
        }

What am i missing? 我错过了什么? (yes im new to this!) tnx (是的,我是新手!)tnx

javascript: var xmlhttp; 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();

HTML (api.aspx) HTML(api.aspx)

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

CODE BEHIND (api.aspx) 代码背后(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);
    }
}

Thats how I have consumed it in test.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>

I am getting the xml as expected. 我按预期得到了xml。 Please test and let me know if it helps. 请测试并告诉我它是否有帮助。

You'll need to set the content type of the response in order for browsers to process it correctly: 您需要设置响应的内容类型,以便浏览器正确处理它:

Response.ContentType = "text/xml";

As Tariqulazam said, the page content is probably okay. 正如Tariqulazam所说,页面内容可能还可以。 To see what's really happening, look at it with "View Page Source" rather than in the dev tools. 要查看实际发生的情况,请使用“查看页面源”而不是开发工具查看它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM