views:

854

answers:

4

Hi I write a method on my page that return a XMLDocument.This is my method:

    [System.Web.Services.WebMethod()]
public static System.Xml.XmlDocument MyGet()
{
    string cnn=System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString.ToString();
    SqlConnection cn = new SqlConnection(cnn);
    SqlCommand cmd = new SqlCommand("select * from region", cn);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    cn.Open();
    DataSet dt = new DataSet();
    da.Fill(dt);
    XmlDocument xdoc=new XmlDocument();
    xdoc.LoadXml(dt.GetXml());
    return xdoc;
}

and this is my jQuery Ajax code:

            $.ajax({
            type: "POST",
            url: "Default2.aspx/MyGet",
            data: "{}",
            dataType: "xml",
            success: function(result) {
                $(result).find("Table").each(function() {
                    alert($(this).find("RegionID").text());
                });

            }

but it does not work. If I write MyGet method in Web Service it works very fine. where is the problem? thanks alot

+1  A: 

Try adding the following attribute to your service operation:

[ScriptMethod(ResponseFormat=ResponseFormat.Xml)]
Rob Windsor
A: 

I suggest you get rid of the {} in your data parameter. It's likely that the serializer thinks you want json back, not xml.

ScottE
A: 

My suggestion is that try Jquery plugins that converts xml to json. Your problem is with Jquery api not with asp.net.

Efe Kaptan
A: 

A few suggestions:

1) Shouldn't the "{}" be without quotes?

2) You could try alert(response); inside your success function to see what kind of data jQuery thinks it has. It could be text.. I've had unexpected data types before with jQuery.

3) You could try $("Table", result) instead of $(result).find("Table")

Hope this helps

Seb Woolford