【问题标题】:C# Web method is not calling in javascriptC# Web 方法未在 javascript 中调用
【发布时间】:2015-03-11 02:53:28
【问题描述】:

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

网页方法代码为:

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

    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# 方法类的确切路径,但它不起作用,但在控制台上给出了一个错误,任何人都可以建议我在这里缺少什么..

【问题讨论】:

  • 从 ajax 的 url 路径中删除第一个斜杠 (/) 并尝试
  • 试过了,没解决我的问题
  • 查看文件夹在sitecore文件夹中是什么?
  • 设为public static int ItemCount(string itemId) 并删除Request 用法、dataType: "json"contentType: "application/json; charset=utf-8type: "POST",如下例所示。
  • 你也在使用data: JSON.stringify({ itemId: itemId }),而不是带有查询字符串的url?

标签: javascript c# jquery json asp.net-mvc


【解决方案1】:

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

  • 您的 WebMethod 应该是 publicstatic
  • 如果您的 WebMethod 需要一些参数而不是这些参数,则必须在 ajax 中以 data 的形式传递。
  • WebMethoddata ajax 部分中的参数名称应为same
  • 从 ajax 传递的数据应该在json string 中。为此,您可以使用JSON.stringify 或者您必须将values 的参数包含在quotes 中。

请检查以下示例 ajax 调用

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 请求中调用您的 web 方法。在 WebMetod 之上添加[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]

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

【讨论】:

  • 我的网页在一个文件夹中,那么我是否需要在 URL 中添加它的名称
  • 不,它正在查找页面,但您需要将 WebMethod 设为静态。
  • 我在将方法更改为静态时遇到同样的错误
  • 当我在演示项目中工作并在 javascript 中调用示例 C# 方法时它调用正常,但同一页面上有 c# 方法
【解决方案2】:

只需修改javascript函数如下

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

【讨论】:

    【解决方案3】:

    我不确定它是否是针对每个问题主题的解决方案,哪个 Web 方法不会触发。但是我今天发现字符串中有 ' 字符。 Web 方法未触发。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多