2016-05-17 51 views
2

我有一个我想在客户端调用的c#方法。 我用AJAX调用来实现这个无法使用ajax从javascript调用c#方法

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

但Web方法不被解雇。 我也尝试使用pagemethods.methodname()作为替代方法(通过使用enablepagemethods = true注册脚本),但没有任何效果。

如果有人能指导我在哪里做错了? 在下面的图片中,您可以清楚地看到.. ,您可以看到跳过ajax调用的断点执行。 enter image description here

+0

尝试在此方法所在的类上添加此[System.Web.Script.Services.ScriptService] –

+0

在浏览器的'console'上得到了什么? –

+0

@guruprasad rao没有什么我在浏览器上,当我调试和验证,控制不传递给方法。 –

回答

0

似乎有问题,你通过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; 


      } 
1

我今天面临类似的问题。在我来说,我在App_start文件夹中有RouteConfig,所以我通过评论这条线

//settings.AutoRedirectMode = RedirectMode.Permanent;

这是问题的根源解决我的问题。

您的网络方法需要公开和静态

public static int MethodName() 
{ 
    // your method 
} 
+0

我没有使用MVC,所以在我的情况下,这不适用。谢谢。如果任何其他建议,它将是伟大的 –

+0

以及我也没有使用MVC,你也可以在Webforms中使用'RouteConfig',但是对于你的问题,请检查已更新的答案 – KanisXXX

0

您的JavaScript的一个微小的错字。请记住,javascript区分大小写。您传递:

Type: "POST" 

但事实上,你应该传递:

type: "POST" 

如果您使用的网络选项卡上的F12,你会发现你的当前AJAX调用一个GET请求,而不是POST请求。