简体   繁体   English

如何将web服务中的xml数据从webform返回到jquery ajax调用

[英]How to return xml data from webservice to jquery ajax call from a webform

This is my JQuery AJAX call. 这是我的JQuery AJAX调用。 This is inside the document.ready() function. 这是在document.ready()函数内部。 This is supposedly the one that will read the xml data returned by the webmethod in my webservice: 这应该是读取我的webservice中webmethod返回的xml数据的那个:

            $.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. 这是我在webservice中的webmethod调用。 I am able create xml sucess fully but i am finding difficulty in returning xml back to ajax call. 我能够完全创建xml成功,但我发现很难将xml返回到ajax调用。

    [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;
    }

With a web service you need not build your xml manually like you are doing. 使用Web服务,您无需像操作那样手动构建xml。 What you should be doing is returning your c# objects in their raw form. 你应该做的是以原始形式返回你的c#对象。 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. ASP.NET会为您将对象序列化为xml。

Also in your javascript you'll need to parse the xml before using .find using something like so: 同样在你的javascript中你需要在使用.find之前解析xml,使用类似的东西:

.success(function(data){
   var xml = $.parseXml(data);
   xml.find(yadayadayada.....
});

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. 当我使用CDN时,错误函数中的第三个参数表示映射中存在某种异常。 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! 我使用google搜索:“失败nsresult:”0x80004005(NS_ERROR_FAILURE)“但只能找到类似的错误。我在IE10中试过它,它的工作原理!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM