2017-12-06 2 views
0

Ich habe einen einfachen ASMX-Webservice mit einer Methode geschrieben, um einen Wert aus der Datenbank zu erhalten. Mein Service-Code ist:Wie man jQuery verwendet, um asp.net asmx Web Service mit Parametern zu rufen, um Antwort zu erhalten

[WebMethod] 
public string GetProductStock(string productId, string TerminalId, string CCPId)   
{ 
    ProductsDomain objProduct = new ProductsDomain(); 
    objProduct.Product_Id = Convert.ToInt32(productId); 
    objProduct.TerminalId = Convert.ToInt32(TerminalId); 
    objProduct.CCPId = Convert.ToInt32(CCPId); 
    DataTable dt = objProduct.GetProductDetail(); 
    if (dt.Rows.Count > 0) 
    { 
     return dt.Rows[0]["CurrentQty"].ToString(); 
    } 
    else 
    { 
     return "0"; 
    } 
} 

und dies ist meine Jquery-Funktion:

function checkStock() { 
      var txt_PId = $("#content_body_content_lPId").text(); 
      var txt_TerminalId = $("#content_body_content_lTerminalId").text(); 
      var txt_CCPId = $("#content_body_content_lCCPId").text(); 
      var msg = "{" + String.format("'productId':'{0}', 'TerminalId':'{1}','CCPId':'{2}'", txt_PId, txt_TerminalId, txt_CCPId) + "}" 
      // alert(msg); 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "CService.asmx/GetProductStock", 
       data: msg, 
       dataType: "json", 
       success: function (Result) { 
        alert(); 
        Result = Result.d; 
        // data = Result 
        //alert(data) 
       }, 
       error: function (Result) { 
        debugger; 
        alert("Error: " + Result.error.toString()); 
        return false; 
       } 
      }); 
      return false; 
     } 

diese jquery-Methode zurückkehrt Fehler, und wenn ich den Fehler auf Fehler, Fehlerstatuscode ist 500. Fehlermeldung:

Error: function(){if(h){var d=h.length;!function f(b){n.each(b,function(b,c){var d=n.type(c);"function"===d?a.unique&&k.has(c)||h.push(c):c&&c.length&&"string"!==d&&f(c)})}(arguments),b?e=h.length:c&&(g=d,j(c))}return this}

Jeder kann mir eine Lösung vorschlagen?

+0

über seine Methoden zuzugreifen Was ist Fehlermeldung? – SeM

+0

@SeM Fehlermeldung in die Frage –

+0

hinzugefügt müssen Sie 'JsonConvert.SerializeObject' verwenden, um das Rückgabeobjekt zu serialisieren – RonyLoud

Antwort

1

[System.Web.Script.Services.ScriptService] haben auf dem classSystem.Web.Services.WebService vererben werden aktiviert ajax

WebService

[WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService] 
    public class WebServiceFile : System.Web.Services.WebService 
    { 

     [WebMethod] 
     //[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 
     public string GetProductStock(string productId, string TerminalId, string CCPId) 
     { 
      ProductsDomain objProduct = new ProductsDomain(); 
      objProduct.Product_Id = Convert.ToInt32(productId); 
      objProduct.TerminalId = Convert.ToInt32(TerminalId); 
      objProduct.CCPId = Convert.ToInt32(CCPId); 
      DataTable dt = objProduct.GetProductDetail(); 
      if (dt.Rows.Count > 0) 
      { 
       return dt.Rows[0]["CurrentQty"].ToString(); 
      } 
      else 
      { 
       return "0"; 
      } 
     } 

     public class ProductsDomain 
     { 
      public int Product_Id { get; set; } 
      public int TerminalId { get; set; } 
      public int CCPId { get; set; } 
      public DataTable GetProductDetail() 
      { 
       DataTable dt = new DataTable(); 
       dt.Columns.Add("CurrentQty"); 
       DataRow dr = dt.NewRow(); 
       dr["CurrentQty"] = "Smith"; 
       dt.Rows.Add(dr); 
       DataRow dr1 = dt.NewRow(); 
       dr1["CurrentQty"] = "John"; 
       dt.Rows.Add(dr1); 
       return dt; 

      } 
     } 

    } 

JQuery

function checkStock() { 
      var txt_PId = 1; 
      var txt_TerminalId = 2; 
      var txt_CCPId = 3; 
      var msg = '{productId:"' + txt_PId + '",TerminalId:"' + txt_TerminalId + '",CCPId:"' + txt_CCPId + '"}'; 
      debugger; 
      $.ajax({ 
       type: "POST", 
       cache: false, 
       contentType: "application/json; charset=utf-8", 
       url: "/WebServiceFile.asmx/GetProductStock", 
       data: msg, 
       dataType: "json",     
       success: function (Result) { 
        alert(); 
        Result = Result.d; 
        // data = Result 
        alert(Result) 
       }, 
       error: function (Result) { 
        debugger; 
        alert("Error: " + Result.error.toString()); 
        return false; 
       } 
      }); 
     } 
+0

großer Mann ... Sie gerade gelöst Das Thema... –

Verwandte Themen