从jquery ajax调用c#方法后面的代码

时间:2013-12-03 09:14:43

标签: c# asp.net

我试图从jquery ajax调用调用代码隐藏方法,但它并没有达到该方法的断点。我错过了什么?

JS

$('#btn_Submit').click(function () {
    $.ajax({
            url: 'ajaxExecute.aspx/CAO2',
            data: 'Guardian=' + $('#txtGuardianName').val() + '&CIFID=' + $('#txtCIFID').val() + '&EmploymentType=' + encodeURIComponent($('#cmbEmploymentType').val()) + '&NatureIncome=' + encodeURIComponent($('#cmbIncomeNature').val()) + '&Occupation=' + encodeURIComponent($('#cmbOccupation').val()),
            cache: false,
            context: document.body,
            type: 'POST',
            success: function (response) {

            }
        });
}

ajaxExecute.aspx.cs

public partial class ajaxExecute : System.Web.UI.Page
{
    [WebMethod]
    public static void CAO2(string Guardian, string CIFID, string EmploymentType, string NatureIncome, string Occupation)
    {
            //Some Code
        //No breakpoint hit here
    }

    protected void Page_Load(object sender, EventArgs e)
    {
            //Some Code
        // Got Breakpoint here
    }
}

3 个答案:

答案 0 :(得分:1)

尝试以JSON格式发送数据:

$('#btn_Submit').click(function () {

   var request = {
     Guardian : $('#txtGuardianName').val(),
     CIFID : $('#txtCIFID').val(),
     EmploymentType : encodeURIComponent($('#cmbEmploymentType').val(),
     NatureIncome : encodeURIComponent($('#cmbIncomeNature').val()),
     Occupation : encodeURIComponent($('#cmbOccupation').val()
   };    

   var strRequest = JSON.stringify(request);  

   $.ajax({
            url: 'ajaxExecute.aspx/CAO2',
            data: strRequest,
            dataType: "json",
            contentType: "application/json",  
            cache: false,
            context: document.body,
            type: 'POST',
            success: function (response) {

            }
        });
   }

答案 1 :(得分:1)

尝试以下

var dataString = JSON.stringify({ 
    Guardian : $('#txtGuardianName').val(), 
    CIFID : $('#txtCIFID').val(), 
    EmploymentType : encodeURIComponent($('#cmbEmploymentType').val(),
    NatureIncome : encodeURIComponent($('#cmbIncomeNature').val()),
    Occupation : encodeURIComponent($('#cmbOccupation').val()
});

$.ajax({
    type: "POST",
    url: "ajaxExecute.aspx/CAO2",
    data: dataString,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
       alert("Success");
    }
});

答案 2 :(得分:1)

尝试Json-ify你输入数据 像(未经测试)的东西: data: JSON.stringify({ Guardian: $('#txtGuardianName').val(), CIFID: $('#txtCIFID').val() ... })