Asp.Net Jquery Ajax Calling Code-Behind Method

ASP.NET jQuery Ajax Calling Code-Behind Method

Firstly, you probably want to add a return false; to the bottom of your Submit() method in JavaScript (so it stops the submit, since you're handling it in AJAX).

You're connecting to the complete event, not the success event - there's a significant difference and that's why your debugging results aren't as expected. Also, I've never made the signature methods match yours, and I've always provided a contentType and dataType. For example:

$.ajax({
type: "POST",
url: "Default.aspx/OnSubmit",
data: dataValue,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
},
success: function (result) {
alert("We returned: " + result);
}
});

calling code behind c# method from jquery ajax

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) {

}
});
}

can I use ajax to call C# code behind method?

You can use the WebMethod attribute

Pagemethods in asp.net

Using jQuery AJAX to call ASP.NET function in control code-behind instead of page code-behind

You can't...WebMethods have to be in WebServices or Pages, they cannot be inside UserControls.

Think about it a different way to see the issue a bit clearer...what's the URL to a UserControl? Since there's no way to access them you can't get at the method directly. You could try another way perhaps, maybe a proxy method in your page?

Calling of Code Behind method from ajax function Jquery not working

Try using {} for your data, "{}" will be considered as string or don't include data parameter if you are not sending any parameters.

onDelete: function (item) {
//Some Code
$.ajax({
type: "POST",
url: 'Page_Name.aspx/callmethod',
// data: "{}",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json"
});
}

Here

call asp.net web forms code behind using ajax

After hours of trying and research I found the solution and here are my full code:

 $(document).ready(function () {
$('#Text2').change(function () {
var ema = $('#Text2').val();
$.ajax({
type: "POST",
url: "Login.aspx/GetDoublicate",
// data: '{"email":'+$('#Text2').val()+ ',}',
data: JSON.stringify({ "email":ema }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// console.log(response.d);
if(response.d == true)
{ alert("doublicate email discovered"); }
else {alert("Ok, go on")};
}
,
error: function (xhr, err) { alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status + "\nresponseText: " + xhr.responseText); }

});


})

})

noting that jSon parameters must be same name of function called parameter.

here asp code:
[WebMethod]
public static bool GetDoublicate(string email)
{

        SqlConnection con = new SqlConnection(connectionString);
con.Open();
string sqltext = "select id from CoAuthor where email='" + email + "'";
SqlCommand cmd = new SqlCommand(sqltext, con);
SqlDataReader dr= cmd.ExecuteReader();
while (dr.Read())
{
return true;
}
con.Close();
return false;


}

Jquery AJAX doesn't call a code behind method

You don't need to convert result to JSON, it is auto. serialized to JSON

 [WebMethod()]
public static object GetData()
{
return (new { NewTotal = "777", OfType = "love" });
}

On JS, refer to newTotal like this

data.d.NewTotal


Related Topics



Leave a reply



Submit