Calling server side functions using ajax in javascript
In the javascript side
<script>
function insertValue(){
$.ajax({
type: "POST",
url: "Default.aspx/serverFunction",
data: '{path: "' + path + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(){
},
failure: function (response) {
alert(response.d);
}
});
}
}
</script>
In the code section we use a webmethod to insert value
[System.Web.Services.WebMethod(EnableSession = true)]
public static string serverFunction(string path)
{
SqlConnection con = new SqlConnection(connection.getconnection());
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
// Your query here
return "";
}
<script>
function insertValue(){
$.ajax({
type: "POST",
url: "Default.aspx/serverFunction",
data: '{path: "' + path + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function OnSuccess(){
},
failure: function (response) {
alert(response.d);
}
});
}
}
</script>
In the code section we use a webmethod to insert value
[System.Web.Services.WebMethod(EnableSession = true)]
public static string serverFunction(string path)
{
SqlConnection con = new SqlConnection(connection.getconnection());
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
// Your query here
return "";
}
Comments
Post a Comment