Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
    }
share|improve this question

2 Answers

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.....
});
share|improve this answer
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' – Nizzy Jun 26 at 20:44
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 – bluetoft Jun 26 at 20:58
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? – bluetoft Jun 26 at 21:01
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. – Nizzy Jun 26 at 22:00

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.