1

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: https://i.sstatic.net/7Yv9z.jpg

If I go to the url in with my browser it displays the XML correct, so I guess its no erros with the source?

Heres the code thats called on 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

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();
8
  • Oh, im using asp.net, and above code is in codebehind. the aspx page only has this <%@ Page Language="C#" Inherits="projekt.api" %> Commented Oct 17, 2012 at 22:06
  • 1
    I think everything is fine there. Its how chrome handles XML file. Did you try with FireFox? Commented Oct 17, 2012 at 22:51
  • ye, same :/ when i console.log(xmlhttp.responseText) with ajax the string looks good, but when using xmlhttp.responseXML i get null.. Commented Oct 17, 2012 at 23:01
  • Please post your javascript code. Commented Oct 17, 2012 at 23:02
  • As Kevin mentioned, add ContentType="text/xml" in the .aspx page directive and it should be alright. Commented Oct 17, 2012 at 23:18

2 Answers 2

3

HTML (api.aspx)

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

CODE BEHIND (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

<%@ 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. Please test and let me know if it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

IT WORKS! it was the ContentType="text/xml" in api.aspx that did it! oh man! you are my HERO! thank you so veeeeeery much! :) <%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" ContentType="text/xml" %>
I am going to give +1 to Kevin's solution as technically it is the same thing he mentioned and I am glad that it worked.
Thanks Tariqulazam. Good call on the page directive.
1

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. To see what's really happening, look at it with "View Page Source" rather than in the dev tools.

3 Comments

i have set the .ContentType above it think (3rd row)? If i look at it in Page source it looks as I want! :) but when I try to get it with an Ajax-req the xmlhttp.responseXML gets null, but the xmlhttp.responseText looks like the xml i want :( any idea?
It looks to me like you're setting the content type of the request, not the response. I think you want to set resp.ContentType instead of req.ContentType.
Just shooting from the hip, you could try setting the content type to "text/xml; charset=utf-8" or to "text/xml". Probably won't make a difference since the error indicates a problem on the server side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.