2012-10-17 54 views
1

我試圖得到一個XML,然後response.write它到一個頁面(在我的服務器上),所以我可以在稍後使用Ajax請求(javascript)來獲取它。但是,當我嘗試這個文件出來一個HTML的頁面與XML節點: http://imgur.com/GL47UResponse.Write webrequest不能正確顯示XML

如果我去的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: 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(); 
+0

哦,即時通訊使用asp.net,上面的代碼是在代碼隱藏。 aspx頁面只有這個<%@ Page Language =「C#」Inherits =「projekt.api」%> – tobbe

+1

我認爲那裏一切都很好。它的chrome如何處理XML文件。你嘗試過使用FireFox嗎? – Tariqulazam

+0

你也一樣:/當我console.log(xmlhttp.responseText)與ajax的字符串看起來不錯,但是當使用xmlhttp.responseXML我得到空.. – tobbe

回答

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。請測試並讓我知道它是否有幫助。

+0

IT WORKS!它是ContentType =「text/xml 「in api.aspx that do it! oh man!你是我的英雄!非常感謝你!:) <%@ Page Title =「主頁」語言=「C#」AutoEventWireup =「true」 CodeFile =「Default.aspx.cs」Inherits =「_ Default」** ContentType =「text/xml」** %> – tobbe

+0

我打算給凱文的解決方案+1,因爲它在技術上與他提到的是同一件事,我很高興它的工作。 – Tariqulazam

+0

謝謝Tariqulazam。對頁面指令有很好的調用。 – Kevin

1

你需要設置響應的內容類型,以便瀏覽器能夠正確地進行處理:

Response.ContentType = "text/xml"; 

正如Tariqulazam說,頁面內容也沒什麼問題。要查看實際發生的情況,請使用「查看頁面源代碼」而不是在開發工具中查看。

+0

我已經在它上面設置了.ContentType(第三行)?如果我在頁面源中查看它,它看起來就像我想要的! :)但是當我嘗試使用Ajax-req獲取它時,xmlhttp.responseXML變爲null,但xmlhttp.responseText看起來像我希望的xml :(任何想法? – tobbe

+1

它在我看來就像設置內容類型我認爲你想設置resp.ContentType而不是req.ContentType。 – Kevin

+0

ahhh,但現在我得到錯誤500,而不是:/ – tobbe