【问题标题】:Getting jQuery ajax and asp.net webmethod xml response to work让 jQuery ajax 和 asp.net webmethod xml 响应正常工作
【发布时间】:2011-05-28 17:50:03
【问题描述】:

我有一个返回 XmlDocument 对象的 asp.net WebMethod。 我可以使用 jquery ajax 成功调用该方法,但似乎无法使函数成功(使用正确的参数调用服务器端 webmethod,但客户端方法因“未定义的解析器错误”而失败)。

为了重现,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); }
    });
}

如果 dataType: "xml" 被注释掉(自动),则调用成功函数,但数据是方括号的负载,似乎表示空的 json 结构。 我想要一个可以使用 jQuery 解析的 XML 响应。

我想我可能需要在 XML 文档中添加一些格式标识,但不确定。谁能指出问题所在?

【问题讨论】:

  • 弗雷德里克的答案去哪儿了?

标签: asp.net jquery xml ajax web-services


【解决方案1】:

通过添加修复

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

到网络方法。

感谢 riteshtandon23 在 this thread

【讨论】:

    猜你喜欢
    • 2012-10-28
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    • 1970-01-01
    • 2011-02-08
    相关资源
    最近更新 更多