後端程式:webservice取得由網頁送過來的String Array(JSON format)
網頁程式:jQuery+Ajax+JSON 送出checkbox所覆選的學號(JSON format)至webservice程式

資料轉換:利用 JSON.stringify來轉換成JSON format
var jsonText = JSON.stringify({ students: studs});

JSON format: { "students": ["No0001","No0002","No0003"]}



Webservice端程式:

   Public Function AddMyData(ByVal students As List(Of String)) As String
      ' 程式碼
   End Function



網頁端程式:
 $('#btnSubmit').click(function() {
    if (confirm("確定新增?")) {

   // 取得myCheckbox所勾選的學號
    var studs= new Array();
    $('input[name="myCheckbox"]:checked').each(function() {
    studs.push($(this).val());
    });
 
  //送出前轉換成JSON格式
  var jsonText = JSON.stringify({ students: studs});

 $.ajax({
    type:"POST",
   
url:"myWS.asmx/AddData",
    data: jsonText,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response) {
    //程式碼
    // 為什麼要.d才能取得JSON資料,請點我
    alert(response.d);
    }, failure: function(msg) {
    alert(msg.responseText);
    }, error: function(msg) {
    alert(msg.responseText);
    }
  });
 }
});


延伸閱讀:[c#]asp.net+jQuery+json做Ajax

arrow
arrow
    全站熱搜

    湯瑪的吳 發表在 痞客邦 留言(0) 人氣()