I have a simple code to update the database with simple Ajax call using WebMethod but its not updating in the database.
HTML Markup
<a href="#" class="newProject" id ="<%= item2 %>"><i class="fa fa-file"></i> <%= item2 %></a>
Client Side Method:
<script type="text/javascript">
$(".newProject").on("click", function () {
$.ajax({
type: "POST",
url: "index.aspx/UpdateCode",
data: 'engCode=' + this.id,
success: function (response) {
// window.location.reload();
alert(response);
}
});
return false;
});
</script>
Server side method:
[WebMethod]
public static int UpdateCode(string Code)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("Codes"))
{
int intresult = 0;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Mode", SqlDbType.VarChar, 50).Value = "UpdateCodes";
cmd.Parameters.Add("@Code", SqlDbType.VarChar, 50).Value = Code;
cmd.Connection = con;
try
{
con.Open();
intresult = cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
ex.ToString();
}
finally
{
cmd.Dispose();
if (con != null)
{
con.Close();
}
}
return intresult;
}
}
}
Its posting the right value on click event of hyperlink but not updating the database.
ASP.NET WebMethods do only work with JSON as the data format:
In your Ajax call, you need to add
dataType: "json",
and also supply the data as JSON:
data: {"engCode":this.id},
you are not passing data in correct formate. try this
<script type="text/javascript">
$(".newProject").on("click", function () {
$.ajax({
type: "POST",
url: "index.aspx/UpdateCode",
data: {Code: this.id}
success: function (response) {
// window.location.reload();
alert(response);
}
});
return false;
});
</script>
Try this code also....
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="JavaScriptSample._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How to call CSharp function in Ajax | JavaScript Sample</title>
<script type="text/javascript">
function getServerTime() {
/// <summary>
/// Call ServerTime web method via ajax request
/// </summary>
if (window.XMLHttpRequest) {
// for IE7+, Firefox, Chrome, Opera, Safari
this.xmlhttp = new XMLHttpRequest();
}
else {
// for IE6, IE5
try {
this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e1) {
try {
// older version of Msxml
this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
this.xmlhttp = null;
}
}
}
xmlhttp.onreadystatechange = function() {
/// <summary>
/// Display server time when success
/// </summary>
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// success Status
document.getElementById("ServerTimeResponse").innerHTML = xmlhttp.responseText;
}
}
this.xmlhttp.open("POST", "AjaxServer.asmx/ServerTime", true);
this.xmlhttp.send();
}
</script>
</head>
<body>
<form id="ServerTimeForm" runat="server">
<div>
<input id="ServerTimeButton" type="button" onclick="getServerTime();" value="Get Server Time" />
<div id="ServerTimeResponse" runat="server">
</div>
</div>
</form>
</body>
</html>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.