Amit Sharma
Amit Sharma

Reputation: 1088

C# Web method is not calling in javascript

enter image description herei create a web method and now i'm calling this in my java script file but it give an path error,it is not able to find path what i'm giving ..

Web method code is :

    [System.Web.Services.WebMethod]
    public static int ItemCount(string itemId)
    {
        int val = 0;

            Item itm = Sitecore.Context.Database.GetItem(itemId);
            val = itm.Children.Count;

        return val;
    }

java script function calling like as:

    function GetItemCount(itemId) {
    var funRes = "";
    debugger;
    try {
    if (itemId != null) {
        jQuery.ajax({
            cache: false,
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Views/GetItem.aspx/ItemCount",
            data: { itemId: itemId },
            dataType: "json",
            async: false,
            success: function (data) {
                funRes = data.result;
            },
            error: function(err) {
                alert(err.responseText);
            }
        });
    }
  } catch (ex) {
    alert(ex.message);
  }
  return funRes;}

while i'm giving exact path for the C# method class but it's not working give an error on console, can anyone suggest me what i'm missing here..

Upvotes: 0

Views: 6085

Answers (3)

Semih Can Bilgen
Semih Can Bilgen

Reputation: 414

I am not sure it is a solution for every question subject which web method not fire. But I discovered today when there is ' char in the string. Web Method not firing.

Upvotes: 0

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14604

There are few rules for ajax to work with asp.net.

  • Your WebMethod should be public and static.
  • If your WebMethod expects some parameter(s) than these parameter(s) must be passed as data in ajax.
  • Name of parameter(s) should be same in WebMethod and in data part of ajax.
  • Data passed from ajax should be in json string.For this you can use JSON.stringify or you will have to surround the values of parameter(s) in quotes.

Please check the below sample ajax call

function CallAjax()
    {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Default.aspx/CallAjax",
            data: JSON.stringify({ name: "Mairaj", value: "12" }),
            dataType: "json",
            async: false,
            success: function (data) {
                //your code

            },
            error: function (err) {
                alert(err.responseText);
            }

        });
    }



[WebMethod]
public static List<string> CallAjax(string name,int value)
{
    List<string> list = new List<string>();
    try
    {
        list.Add("Mairaj");
        list.Add("Ahmad");
        list.Add("Minhas");
    }

    catch (Exception ex)
    {

    }

    return list;
}

EDIT

If you use GET in ajax than you need to enable your webmethod to be called from GET request. Add [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)] on top of WebMetod

[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static int ItemCount()

Upvotes: 14

Sandip Rabade
Sandip Rabade

Reputation: 33

Just Modify the javascript function as below

  function GetItemCount(itemId) {
    var funRes = "";
   debugger;
   try {
    if (itemId != null) {
        jQuery.ajax({
            type: "GET",
            url: "/Views/GetItem.aspx",
            data: 'itemID=' + itemId,
            contentType: "application/html",
            dataType: "html",
            success: function (response) {
                funRes= response.result;
            }
        });
     }
  } catch (ex) {
    alert(ex.message);
  }
  return funRes;
}

Upvotes: 0

Related Questions