0

This is my JQuery AJAX call. This is inside the document.ready() function. This is supposedly the one that will read the xml data returned by the webmethod in my webservice:

            $.ajax({
                type: "POST",
                url: "http://tempuri.org/NewsletterList.asmx/HelloWorld",
                contentType: "application/xml; charset=utf-8",
                dataType: "xml",
                success: function (xml) {
                    $(xml).find('Newsletter').each(function () {
                        var title = $(this).find('Title').text();
                        var created = $(this).find('Created').text();
                        AddOption(title);
                        alert('Ywes');
                    });
                },
                error: function (msg, m2, m3) {
                    alert(m2);
                }
            });

This is my webmethod call in my webservice. I am able create xml sucess fully but i am finding difficulty in returning xml back to ajax call.

    [WebService(Namespace = "http://tempuri.org/")]
    .
    .
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public XmlDocument HelloWorld()
    {
        //Instantiate model object
        nl = new Newsletter();

        //Initiate XML stuff
        StringBuilder sb = new StringBuilder();
        XmlWriterSettings settings = new XmlWriterSettings();
        XmlWriter writer = XmlWriter.Create(sb, settings);

        writer.WriteStartDocument();
        writer.WriteStartElement("Root");

        foreach (Newsletter nls in nl.GetNewsletterList())
        {
            writer.WriteStartElement("Newsletter");
            writer.WriteElementString("Title", nls.Title);
            writer.WriteElementString("Created", nls.Created.ToString());
            writer.WriteEndElement();
        }

        writer.WriteEndElement();
        writer.WriteEndDocument();
        writer.Flush();

        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(sb.ToString());
        return xmlDocument;
    }

2 Answers 2

2

With a web service you need not build your xml manually like you are doing. What you should be doing is returning your c# objects in their raw form. You should have the following instead:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public List<Newsletter> HelloWorld()
{
    //Instantiate model object
    return new Newsletter().GetNewsletterList();


}

ASP.NET will serialize your objects to xml for you.

Also in your javascript you'll need to parse the xml before using .find using something like so:

.success(function(data){
   var xml = $.parseXml(data);
   xml.find(yadayadayada.....
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, it did automatically serialize my object, however, it still fails when it gets to the JQuery part, I tried putting in alert the parameter thrown in my error: function but only the second one has text, and it just says 'error'
I don't believe jquery is designed to traverse any ol html fyi. I believe you need to use $.parseXml api.jquery.com/jQuery.parseXML
If you use a network inspector like firebug or chrome dev tools what does the network request look like? Are you hitting your success method or your error method?
Thank you for all your suggestions. It turns out it has something to do w/ the browser. When I use the CDN's, the third parameter in the error functions say that there was some kind of exception in the mapping. I googled it but was only able to find an error similar to it. I tried it in IE10 and it works! Thank you.
0

Thank you for all your suggestions. It turns out it has something to do w/ the browser. When I use the CDN's, the third parameter in the error functions say that there was some kind of exception in the mapping. I googled it using: "Failure nsresult: "0x80004005 (NS_ERROR_FAILURE)" but was only able to find an error similar to it. I tried it in IE10 and it works!

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.