我有一个返回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文档添加一些格式标识,但不确定。谁能指出问题所在?
发布于 2010-12-21 11:20:04
通过添加
[System.Web.Script.Services.ScriptMethod(ResponseFormat=System.Web.Script.Services.ResponseFormat.Xml)]
添加到web方法。
在this thread中贷记给riteshtandon23
https://stackoverflow.com/questions/4498156
复制