Usman Farooq
Usman Farooq

Reputation: 109

How to use jQuery to call asp.net asmx web service with parameters to get response

I have written a simple ASMX web service with a method to get some value from the database. My service code is:

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

and this is my jquery function:

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

this jquery method is returning error, and when i debugged the error, error status code is 500. Error message:

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}

Any one can suggest me a solution to this?

Upvotes: 0

Views: 1742

Answers (1)

RonyLoud
RonyLoud

Reputation: 2426

[System.Web.Script.Services.ScriptService] have to be enabled on the class inheriting System.Web.Services.WebService to access its methods via 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;
                }
            });
        }

Upvotes: 1

Related Questions