C# Web 方法不在 javascript 中调用 [英] C# Web method is not calling in javascript

查看:31
本文介绍了C# Web 方法不在 javascript 中调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 web 方法,现在我在我的 java 脚本文件中调用它但它给出了一个路径错误,它无法找到我给出的路径..

i 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 方法代码是:

    [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 脚本函数调用如下:

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

虽然我给出了 C# 方法类的确切路径,但它不起作用在控制台上给出错误,谁能告诉我我在这里遗漏了什么..

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

推荐答案

ajax 与 asp.net 一起工作的规则很少.

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

  • 您的 WebMethod 应该是 publicstatic.
  • 如果您的 WebMethod 需要一些参数,那么这些参数必须在 ajax 中作为 data 传递.
  • 参数名称在 WebMethoddata 部分应该是 same ajax.
  • 从 ajax 传递的数据应该在 json string 中.为此,您可以使用 JSON.stringify 或者您必须将 values 括起来quotes 中的参数.
  • 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.

请检查下面的示例ajax调用

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

编辑

如果您在 ajax 中使用 GET,那么您需要启用从 GET 请求中调用您的 webmethod.在 WebMetod 之上添加 [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]

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()

这篇关于C# Web 方法不在 javascript 中调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆