2014-09-18 76 views
1

下面調用WebMethod的參數來獲取從客戶端發送的json數組?我用name,但它沒有奏效。使用JQuery Ajax發送Json對象數組到Web方法

var employees = { 
      "accounting": [ // accounting is an array in employees. 
        { 
         "firstName": "", // First element 
         "lastName": "Doe", 
         "age": 23 
        }, 

        { 
         "firstName": "Mary", // Second Element 
         "lastName": "Smith", 
         "age": 32 
        } 
      ], // End "accounting" array.         
      "sales": [ // Sales is another array in employees. 
           { 
            "firstName": "Sally", // First Element 
            "lastName": "Green", 
            "age": 27 
           }, 

           { 
            "firstName": "Jim", // Second Element 
            "lastName": "Galley", 
            "age": 41 
           } 
      ] // End "sales" Array. 
     } // End Employees 

var toServer = JSON.stringify(employees); 

這是jQuery的ajax發送給Web方法。

$("#sendtoServer").click(function() { 
      $.ajax({ 
       type  : "POST", 
       url   : "Default.aspx/GetDetails", 
       data  : '{name: "' + toServer + '" }', 
       contentType : "application/json; charset=utf-8", 
       dataType : "json", 
       success  : OnSuccess, 
       failure  : function (response) { 
        alert("Wrong"); 
       } 
      }); 

      function OnSuccess(response) { 
       alert("Sent"); 
      } 
     }); 

這是Web方法

[System.Web.Services.WebMethod] 
public static string GetDetails(string name) 
{ 
    var x=name; 
    return ""; 
} 
+1

如果您告訴服務器您要發送JSON,則實際上必須發送JSON。 ''{name:''+ toServer +'「}」'不是JSON。也許只需要'data:toServer'。 – 2014-09-18 17:50:18

回答

3

你必須重寫你的數據初始化:

var employees = { 
        accounting: [ // accounting is an array in employees. 
          { 
           firstName: "", // First element 
           lastName: "Doe", 
           age: 23 
          }, 

          { 
           firstName: "Mary", // Second Element 
           lastName: "Smith", 
           age: 32 
          } 
        ], // End "accounting" array.         
        sales: [ // Sales is another array in employees. 
             { 
              firstName: "Sally", // First Element 
              lastName: "Green", 
              age: 27 
             }, 

             { 
              firstName: "Jim", // Second Element 
              lastName: "Galley", 
              age: 41 
             } 
        ] // End "sales" Array. 
       } // End Employees 



       $.ajax({ 
        type: "POST", 
        url: "Default.aspx/GetDetails", 
        data: JSON.stringify({ name: employees }), 
        contentType: "application/json; charset=utf-8", 
        dataType: "json", 
        success: OnSuccess, 
        failure: function (response) { 
         alert("Wrong"); 
        } 
       }); 

       function OnSuccess(response) { 
        alert("Sent"); 
       } 

使用對象參數t YPE服務器端:

[System.Web.Services.WebMethod] 
public static string GetDetails(object name) 
{ 
    var x=name; 
    return ""; 
} 

編輯: 不需要加引號去除作爲@Felix克林指出。

+0

可愛。像魅力一樣工作! – Jude 2014-09-18 18:25:19

+2

@Jude:請注意,不需要刪除屬性名稱中的引號。唯一重要的區別是'data:JSON.stringify({name:employees})'和'GetDetails(object name)'。儘管'data:JSON.stringify(employees)'也足夠了,但不需要爲對象結構添加額外的級別。 – 2014-09-18 18:40:50

2

你要簡單地改變你的Web方法:

[System.Web.Services.WebMethod] 
public static string GetDetails(object name) 
{ 
    var x=name; 
    return ""; 
} 
相關問題