0

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..

10
  • remove first slash (/) from url path for ajax and try Commented Jan 13, 2015 at 7:57
  • tried, not solve my issue Commented Jan 13, 2015 at 8:00
  • view folder is in sitecore folder a what? Commented Jan 13, 2015 at 8:02
  • 1
    Make it public static int ItemCount(string itemId) and remove the Request usage, dataType: "json" and contentType: "application/json; charset=utf-8, and type: "POST" like the example below. Commented Jan 13, 2015 at 8:34
  • 1
    And you're using data: JSON.stringify({ itemId: itemId }) as well, instead of the url with a query string? Commented Jan 13, 2015 at 8:38

3 Answers 3

14

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()
Sign up to request clarification or add additional context in comments.

4 Comments

my web page inside a folder then do i need to add it's name in URL or not
No it is finding the page but you need to make WebMethod static.
i'm getting same error while i changed method to Static
when i'm working in demo project and calling sample C# method in javascript it calling fine, but there c# method on the same page
0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.