簡體   English   中英

從jQuery Ajax在c#方法后面調用代碼

[英]calling code behind c# method from jquery ajax

我正在嘗試從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
    }
}

嘗試將數據作為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) {

            }
        });
   }

嘗試下面

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

嘗試Json驗證您輸入的數據。
類似於(未經測試): data: JSON.stringify({ Guardian: $('#txtGuardianName').val(), CIFID: $('#txtCIFID').val() ... })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM