无法使用 ajax 从 javascript 调用 c# 方法
unable to call a c# method from javascript using ajax
我有一个 c# 方法,我想在客户端调用它。
我使用 ajax 调用来实现这个
function ValidateIfDuplicate()
{
debugger
var billtext = $("#ctl00_ContentPlaceHolder2_textBoxBillNumber").val();
var retailer= $("#ctl00_ContentPlaceHolder2_dropDownListRetailers").val();
var billdate = $("#ctl00_ContentPlaceHolder2_textBoxBillDate").val();
if (billtext == "")
{
alert("Bill Number cannot be left empty");
return false;
}
else if (retailer == 0) {
alert("Please select a Retailer");
return false;
}
else if (billdate == '') {
alert("Date cannot be left empty");
return false;
}
else if (billtext != '' && retailer != '' && billdate != '')
{
$.ajax({
Type: "POST",
url: "CAInvoiceEntry.aspx/ValidateDuplicateEntry",
contentType: "application/json; charset=utf-8",
data: { billtext1: billtext, billdate1: billdate, retailer1: retailer },
dataType: "json",
success: function (result) {
debugger
alert(result.d);
}
});
return true;
}
}
这是我的 C# 方法
[System.Web.Script.Services.ScriptService]
public partial class CAInvoiceEntry: BaseClass
{
[WebMethod, ScriptMethod()]
public static int ValidateDuplicateEntry(string billtext1, string billdate1, string retailer1)
{
string validatebill = objCAInvoiceEntry.validatebilldate(textBoxBillNumber.Text, billdate1.ToString(), ViewState[AppConstants.UploadedBy].ToString(), dropDownListRetailers.SelectedValue);
if (validatebill == "1")
{
return 1;
}
else
return 0;
}
}
但未触发网络方法。
我也尝试使用 pagemethods.methodname() 作为替代方案(通过使用 enablepagemethods=true 注册脚本)但没有效果。
如果有人可以指导我哪里做错了?
只是为了清楚..
在下图中,您可以看到断点执行,其中 ajax 调用被跳过。
我今天遇到了类似的问题。在我的例子中,我在 App_start 文件夹中有 RouteConfig
,所以我通过评论这一行
解决了我的问题
//settings.AutoRedirectMode = RedirectMode.Permanent;
这是问题的原因。
还 您的 Web 方法需要 public 和静态。
类似于
public static int MethodName()
{
// your method
}
您的 javascript 中有一个小错字。请记住 javascript 区分大小写。您正在传递:
Type: "POST"
但实际上你应该传入:
type: "POST"
如果您在“网络”选项卡上使用 F12,您会注意到您当前的 AJAX 调用发出的是 GET 请求,而不是 POST 请求。
您通过 ajax 传递参数的方式似乎存在问题。
试试这个。
if (billtext != '' && retailer != '' && billdate != '')
{
debugger
$.ajax({
type: "POST",
url: "CAInvoiceEntry.aspx/ValidateDuplicateEntry",
data: "{ billtext1:" + "'" + billtext + "'" + ", billdate1:" + "'" + billdate + "'" + ", retailer1:" + "'" + retailer + "'" + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {
if (msg == "1")
alert("Already exists");
else
alert("valid");
},
error: function (msg) {
debugger;
alert(msg);
}
})
return false;
}
我有一个 c# 方法,我想在客户端调用它。 我使用 ajax 调用来实现这个
function ValidateIfDuplicate()
{
debugger
var billtext = $("#ctl00_ContentPlaceHolder2_textBoxBillNumber").val();
var retailer= $("#ctl00_ContentPlaceHolder2_dropDownListRetailers").val();
var billdate = $("#ctl00_ContentPlaceHolder2_textBoxBillDate").val();
if (billtext == "")
{
alert("Bill Number cannot be left empty");
return false;
}
else if (retailer == 0) {
alert("Please select a Retailer");
return false;
}
else if (billdate == '') {
alert("Date cannot be left empty");
return false;
}
else if (billtext != '' && retailer != '' && billdate != '')
{
$.ajax({
Type: "POST",
url: "CAInvoiceEntry.aspx/ValidateDuplicateEntry",
contentType: "application/json; charset=utf-8",
data: { billtext1: billtext, billdate1: billdate, retailer1: retailer },
dataType: "json",
success: function (result) {
debugger
alert(result.d);
}
});
return true;
}
}
这是我的 C# 方法
[System.Web.Script.Services.ScriptService]
public partial class CAInvoiceEntry: BaseClass
{
[WebMethod, ScriptMethod()]
public static int ValidateDuplicateEntry(string billtext1, string billdate1, string retailer1)
{
string validatebill = objCAInvoiceEntry.validatebilldate(textBoxBillNumber.Text, billdate1.ToString(), ViewState[AppConstants.UploadedBy].ToString(), dropDownListRetailers.SelectedValue);
if (validatebill == "1")
{
return 1;
}
else
return 0;
}
}
但未触发网络方法。 我也尝试使用 pagemethods.methodname() 作为替代方案(通过使用 enablepagemethods=true 注册脚本)但没有效果。
如果有人可以指导我哪里做错了?
只是为了清楚..
在下图中,您可以看到断点执行,其中 ajax 调用被跳过。
我今天遇到了类似的问题。在我的例子中,我在 App_start 文件夹中有 RouteConfig
,所以我通过评论这一行
//settings.AutoRedirectMode = RedirectMode.Permanent;
这是问题的原因。
还 您的 Web 方法需要 public 和静态。
类似于
public static int MethodName()
{
// your method
}
您的 javascript 中有一个小错字。请记住 javascript 区分大小写。您正在传递:
Type: "POST"
但实际上你应该传入:
type: "POST"
如果您在“网络”选项卡上使用 F12,您会注意到您当前的 AJAX 调用发出的是 GET 请求,而不是 POST 请求。
您通过 ajax 传递参数的方式似乎存在问题。 试试这个。
if (billtext != '' && retailer != '' && billdate != '')
{
debugger
$.ajax({
type: "POST",
url: "CAInvoiceEntry.aspx/ValidateDuplicateEntry",
data: "{ billtext1:" + "'" + billtext + "'" + ", billdate1:" + "'" + billdate + "'" + ", retailer1:" + "'" + retailer + "'" + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {
if (msg == "1")
alert("Already exists");
else
alert("valid");
},
error: function (msg) {
debugger;
alert(msg);
}
})
return false;
}