4

I have an asp.net WebMethod that returns an XmlDocument object. I can successfully call the method using jquery ajax, but can't seem to get the function to succeed (server side webmethod gets called with correct parameters but client side method fails with 'undefined parser error').

To reproduce, Asp.net C#:

[WebMethod]
public static XmlDocument test(string name)
{
    XmlDocument result = new XmlDocument();
    XmlElement root = result.CreateElement("Data");
    result.AppendChild(root);

    XmlElement element = result.CreateElement("AnElement");
    element.SetAttribute("Name", name);
    root.AppendChild(element);

    return result;
}

JavaScript:

function CallForData(name) {
    $.ajax({
        type: "POST",
        url: "AppName.aspx/test",
        data: "{'name': " + name+ "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "xml",
        success: function (response) { ParseXML(response); },
        error: function (data, textStat, req) { alert(data + ' - ' + textStat + ' - ' + req); }
    });
}

If dataType: "xml" is commented out (automatic) the success function is called but the data is a load of square brackets that seem to indicate an empty json structure. I want an XML response that I can parse using jQuery.

I think I possibly need to add some format identification to the XML document but aren't sure. Can anyone point out the problem?

1
  • Where did Frederic's answer go? Commented Dec 21, 2010 at 11:02

1 Answer 1

5

Fixed by adding

[System.Web.Script.Services.ScriptMethod(ResponseFormat=System.Web.Script.Services.ResponseFormat.Xml)]

to the web method.

Credit to riteshtandon23 in this thread

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

Comments

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.