2010-12-10 127 views
1

通过自定义对象如何使用$阿贾克斯(..)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代码 - 如果将WebMethod的说法不被裁判

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,即上面提到的签名有,我得到(使用firebub出现在服务器响应)的服务器错误 -

No parameterless constructor defined... 

回答

1

这恐怕是不支持的情况。您可以删除ref关键字,并在修改方法内的值后使用参数作为返回类型。

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]  
public MyCustomClass Method1(MyCustomClass MyCustomObj) 
{ 
    MyCustomObj.MyProperty *= 2; 
    return MyCustomObj; 
} 
+0

Thx Darin。我已经尝试过了,它的确如你所说的那样工作。只是希望 – tubelight 2010-12-10 16:08:05

+0

@tubelight,有什么不工作?当我测试它工作正常。我能够在'response.d.MyProperty'中获得更新后的值。 – 2010-12-10 16:09:06

+0

Thx Darin。我已经尝试过了,它的确如你所说的那样工作。只是希望有一些我错过了,如果照顾,它将与一个由ref参考。 – tubelight 2010-12-10 16:13:42