2013-05-13 70 views
2

我有一個jQuery的Ajax的WebMethod呼叫提供的條件從一個WebMethod的值,如下所示:返回基於的WebMethod

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#btnsubmit").click(function() { 

      var arr = new Array(); 
      arr.push($("#control1").val()); arr.push($("#control2").val()); arr.push($("#control13 option:selected").val()); arr.push($("#control4 option:selected").val()); arr.push($("#control15 option:selected").val()); 

      var requestedData = JSON.stringify(arr); 
      requestedData = "{'details':'" + requestedData + "'}"; 

      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "EmployeeDemotion.aspx/Save", 
       data: requestedData, 
       dataType: "json", 
       success: function (result) { 
        //check the value returned from the WebMethod and provide alerts accordingly 

       }, 

       error: function (result) { 
        alert("Error"); 
       } 

      }); 
     }); 

    }); 
</script> 

和,將WebMethod是如下:

[WebMethod(EnableSession = true)] 
    public static InsertDetails[] Save(string details) 
     { 

      DataTable dt = new DataTable(); DataTable dts = new DataTable(); 

      List<InsertDetails> data = new List<InsertDetails>(); 
      JavaScriptSerializer js = new JavaScriptSerializer(); 
      string[] Tags = js.Deserialize<string[]>(details); 
      object[] obj = new object[8]; 
      obj[0] = Tags[0].ToString(); obj[1] = Tags[1].ToString(); obj[2] = Tags[2].ToString(); obj[3] = Tags[3].ToString(); 
      obj[4] = Tags[4].ToString(); 


      int a = //say condition 1 
      int b = //say condition 2 
      if (a< b) 
      { 

      //insert into database and set a value which says the insertion has succeeded 

      } 
      else 
      { 
       //alert that data cannot be inserted 
      } 

      return data.ToArray(); 
     } 

現在我需要將任何可能的類型(布爾,數組,字符串,整數或其他)的值返回給ajax方法,以便ajax方法中的成功函數提醒插入狀態(如te片段中的註釋)即;一個值應該與最後一條語句一起返回「return data.ToArray();」到ajax方法。 我不需要返回元素'data',驗證插入的值應該與'data'一起返回,或者以任何其他形式返回。

回答

4

不知道你想要什麼。您是否希望將數據和標誌返回到客戶端功能或標誌。

  • 案例1:所有你想要的是返回有關把裏面save

    地方只是改變你的保存方法是這樣

    [WebMethod(EnableSession = true)] 
    public static string Save(string details) 
    { 
         string message =string.Empty; 
    
         /// rest of the usual code of yours 
         /// 
         if (a< b) 
         { 
         //rest of the code 
         message = "Insertion Successful"; 
         } 
         else 
         { 
         //rest of the code 
         message = "Error Occured"; 
         } 
    } 
    

    ,並在您的客戶端上的動作的消息裏面的ajax success,簡單做到這一點:

    success: function (result) { 
           alert(result.d); 
    } 
    
  • 案例2:如果插入成功,您還想發送數據

    創建一個包裝並將數據和標誌都附加到它。將其序列化並 然後將其發送到客戶端功能。即

    //wrapper class 
    public class ServiceResponse 
    { 
        public bool IsSuccess {get;set;} 
        public string Message {get;set;} 
    } 
    
    現在

    save內做到這一點:

    [WebMethod(EnableSession = true)] 
    public static string Save(string details) 
    { 
         ServiceResponse serviceResponse =new ServiceResponse(); 
    
         /// rest of the usual code of yours 
         /// 
         if (a< b) 
         { 
         //rest of the code 
          serviceResponse.IsSuccess= true; 
          serviceResponse.Message = String.Join(",",data.ToArray()); 
         } 
         else 
         { 
         //rest of the code 
         serviceResponse.IsSuccess = false; 
         } 
    
        return new JavaScriptSerializer().Serialize(serviceResponse); 
    } 
    

    ,並用它你的客戶端方法中,如:

    success: function (result) { 
          var jsonData = $.parseJSON(result.d); 
          if(jsonData.IsSuccess){ 
           alert('success'); 
           grid.data(jsonData.Message); 
          } 
          else{ 
           alert('failure'); 
          } 
        } 
    
+0

我需要兩個案例1和2 ...謝謝Manish .. – 2013-05-14 05:44:01