0

i am trying to call code behind method from jquery ajax call but it doesnt hit break point on that methhod. what i am missing ?

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
    }
}
10
  • You can't set a breakpoint on a comment. Commented Dec 3, 2013 at 9:16
  • 1
    Fire up Fiddler and see what it tells you. Commented Dec 3, 2013 at 9:17
  • Pass data as JSON string Commented Dec 3, 2013 at 9:18
  • Haven't you forgotten a couple of equal signs? E.g: &CIFID= Commented Dec 3, 2013 at 9:19
  • @Niklas Corrected But of no use :( Commented Dec 3, 2013 at 9:25

3 Answers 3

1

Try to send data as 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) {

            }
        });
   }
Sign up to request clarification or add additional context in comments.

2 Comments

It worked. But why it didnt in my case ? does it because it takes data only in json ?
not sure, but i think that your webmethod waits for 5 strings, but doesn't become them.
1

try below

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

Comments

1

try to Json-ify you input data.
something like (not tested): data: JSON.stringify({ Guardian: $('#txtGuardianName').val(), CIFID: $('#txtCIFID').val() ... })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.