2017-12-06 14 views
0

データベースから値を取得するためのメソッドを使って簡単なASMX Webサービスを作成しました。jQueryを使用してasp.net asmx Webサービスを呼び出してパラメータを取得する方法

[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"; 
    } 
} 

、これが私のjqueryの関数である:私のサービスコードがある

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; 
     } 

このjqueryのメソッドがエラーを返している、と私は、エラーをデバッグするとき、エラーステータスコードが500 エラーメッセージです:

私にはこれに対する解決策がありますか?

+0

エラーメッセージは何ですか? – SeM

+0

@SeMエラーメッセージが質問に追加されました –

+0

戻りオブジェクトをシリアル化するために 'JsonConvert.SerializeObject'を使用する必要があります – RonyLoud

答えて

1

[System.Web.Script.Services.ScriptService]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; 

      } 
     } 

    } 

を経由してそのメソッドにアクセスするためにSystem.Web.Services.WebServiceを継承class上で有効にする必要が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

あなたはただ解決しました問題... –

関連する問題