gpt4 book ai didi

asp.net - 让 jQuery ajax 和 asp.net webmethod xml 响应工作

转载 作者:数据小太阳 更新时间:2023-10-29 02:05:42 24 4
gpt4 key购买 nike

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

要重现,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 文档添加一些格式标识,但不确定。谁能指出问题所在?

最佳答案

通过添加修复

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

到网络方法。

归功于 riteshtandon23 this thread

关于asp.net - 让 jQuery ajax 和 asp.net webmethod xml 响应工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4498156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com