C# Web 方法未在 javascript 中调用
C# Web method is not calling in javascript
我创建了一个网络方法,现在我在我的 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# 方法的确切路径 class 但它不起作用,在控制台上给出错误,任何人都可以建议我我在这里缺少什么..
ajax 使用 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
.
请检查以下示例 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
请求调用您的网络方法。在 WebMetod
之上添加 [System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static int ItemCount()
修改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;
}
我不确定它是否是每个问题主题的解决方案,其中 web 方法不会触发。但是我今天发现字符串中有'char。 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 脚本函数调用如下:
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# 方法的确切路径 class 但它不起作用,在控制台上给出错误,任何人都可以建议我我在这里缺少什么..
ajax 使用 asp.net 的规则很少。
- Your WebMethod should be
public
andstatic
.- 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
inWebMethod
and indata
part of ajax.- Data passed from ajax should be in
json string
.For this you can useJSON.stringify
or you will have to surround thevalues
of parameter(s) inquotes
.
请检查以下示例 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
请求调用您的网络方法。在 WebMetod
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static int ItemCount()
修改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;
}
我不确定它是否是每个问题主题的解决方案,其中 web 方法不会触发。但是我今天发现字符串中有'char。 Web 方法未触发。