2012-02-18 9 views
3

私は2つのjavascript文字列配列 "myArray1"と "myArray2"をajaxからwebmethodに渡したいと思います。私が持っているものは次のもので、うまくいかないものです。複数の配列を渡します。 ajax to webmethod

var myArray1 = new Array();  
var myArray2 = new Array();  
// the arrays are filled further in the code. 
// call to the webmethod: 
$.ajax({ 
     type: "POST", 
     url: "mypage.aspx/SavePage", 
     data: ??????, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(msg) { 
      alert(msg.d); 
     }, 
     error: function() { 
      alert("failed"); 
     } 
    }); 

私はこれらの配列をdata:コールの一部に渡す方法がわかりません。

C#コードの一部です。

public static string SavePage(List<string> myArray1, List<string> myArray2) 
{ 
} 
+0

このhttp://www.aspsnippets.com/Articlesを見てください/Send-and-Receive-JSON-objects-to-Web-Service-Methods-using-jQuery-AJAX-in-ASPNet.aspx – coder

答えて

2

この方法を試してみてください:

あなたの分離コードで
data: "{'myArray1':"+JSON.stringify(myArray1)+",'myArray2':"+JSON.stringify(myArray2)+"}", 

[System.Web.Services.WebMethod] 
public static string SavePage(List<string> myArray1, List<string> myArray2) 
{ 
return myArray1; 
} 
+0

Thあなたはとてもうまく動作します。 – pessi

+0

あなたは大歓迎です!それがあなたのために働くことを喜んで見てください。 – coder

+0

@coder、私はこのEXACTサンプルの成功アラートに続いて(内部サーバーエラー)アラートを取得し続け、Webメソッドのブレークポイントはヒットしません。私はJQuery 1.10.2を使用しています。 –

0
url: "mypage.aspx/SavePage", 
    data: { myArray1: myArray1, myArray2: myArray2 } 
0
var data = {}; 
data.myArray1= myArray1; 
data.myArray1= myArray2; 

var json = JSON.stringify(data); 

$.ajax({ 
    ... 
    data: json, 
    ... 
}); 
関連する問題