【发布时间】:2010-12-10 15:23:45
【问题描述】:
如何使用 $.ajax(..) JSON 为自定义类/对象调用具有“通过引用参数”的 asmx webmethods?可能吗?
我的 c# 代码 -
public class MyCustomClass{ public int MyProperty; MyCustomClass(){}}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public Method1(ref MyCustomClass MyCustomObj)
{ MyCustomObj.MyProperty*=2; return MyCustomObj;}
我的 js/jquery 代码 -
function myCustomClass(){this.myProperty;}
var myCustomObj = new myCustomClass();
myCustomObj.myProperty = 100;
$.ajax({
type: "POST",
data: "{'myCustomObj': " + JSON.stringify(myCustomObj) + "}",
url: "test.asmx/Method1",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
var data = response.d;
alert(data.MyProperty);
},
failure: function(msg) {
alert(msg);
}
});
如果 webmethod 的参数不是 ref,这一切都可以正常工作。 使用上面提到的 webmethod 签名,即使用 by ref 参数,我得到一个服务器错误(在使用 firebub 看到的服务器响应中) -
No parameterless constructor defined...
【问题讨论】:
标签: asp.net jquery ajax web-services