通过Jquery使用Ajax调用函数/数据库更新 [英] Calling a function / database update using Ajax via Jquery
问题描述
我正在创建一个简单的这有用吗?"包含是"和否"对象的表单-使用ASP.net网络表单.
Im creating a simple "Was this useful?" form with Yes and No objects- Using ASP.net webforms.
我需要使用jquery通过ajax进行提交,以防止用户在同一页面上多次投票..目前,在相关页面后面的C#代码中,我有两种方法Like_Click和Dislike_click.
I need the submission to be done via ajax using jquery, to prevent a user from voting multiple times on the same page.. currently i have two methods Like_Click and Dislike_click in the C# code behind the page in question.
任何人都可以通过jquery给我一些指向简单ajax的指针或任何适合的演练的链接(我是ajax的新手!)
Can anyone give me some pointers on or a link to any suitable walkthroughs for simple ajax via jquery (I'm new to ajax!)
我已经研究过在每种方法上使用[WebMethod]标识符,但并没有真正完全理解该方法.
Ive looked at using the [WebMethod] identifier on each of the methods but do not really understand this method fully.
谢谢
推荐答案
您可以尝试以下类似操作
You can try something like below
<script type="text/javascript">
$(function () {
$('#btnSubmit').click(function () {
var like = $('#Like').val();
var dislike = $('#Dislike').val();
if (name != '' && email != '') {
$.ajax
({
type: 'POST',
url: 'Home.aspx/UpdateDB', //UpdateDB is declared as WebMethod
async: false,
data: "{'like':'" + like + "','dislike':'" + dislike + "'}",
contentType: 'application/json; charset =utf-8',
success: function (data) {
var obj = data.d;
if (obj == 'true') {
$('#Like').val('');
$('#Dislike').val('');
alert("Data Saved Successfully");
}
},
error: function (result) {
alert("Error Occured, Try Again");
}
});
}
})
});
</script>
Webmethod如下所示
Webmethod is shown below
[WebMethod]
public static string UpdateDB(string like, string dislike)
{
//Add your stuff
}
在此处查看更多详细信息从ASP.NET中的jquery调用WebMethod
take a look more details here Call WebMethod from jquery in ASP.NET
这篇关于通过Jquery使用Ajax调用函数/数据库更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!