2010-12-10 9 views
1

$ .ajax(..)JSONを使用してカスタムクラス/オブジェクトの 'ref引数'を持つ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属性の引数は参照によってされていない場合は、すべてが正常に動作します。 WebMethod属性の上記署名付き 、即ちREF引数によってで、私は(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

+0

@ tubelight、何が動作しないのですか?私がテストしたところ、正常に動作しました。私は 'response.d.MyProperty'で更新された値を得ることができました。 –

+0

Thx Darin。私はそれを試して、あなたが言及したように動作します。私が見逃しているものがあることを念頭に置いて、気をつけていれば、それはref argで動作します。 – tubelight

関連する問題