这是用于我的聊天应用程序的 Ajax 代码。我已成功完成聊天应用程序,但我需要使用 ajax,以便应用程序减少对数据库的访问,但我不知道如何在我的简单聊天应用程序中使用此代码。请有人详细解释代码的使用
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- Reference to google J-Query api.
You can download and add jquery javasripts files to you soln.-->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<title></title>
</head>
<body>
<script type="text/javascript">
function callService() {
//url of your web service
$.ajax('../sessionOut.asmx/GetNewsAndAlerts',
{
beforeSend: function (xhr) { },
complete: function () { },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
jsonp: 'callback',
type: 'POST',
error: function (xhr, ajaxOptions, thrownError) {
//Function will be called when any error occcured.
alet(thrownError);
},
success: function (data) {
if (!data.d) {
//Cound not find data.
}
else {
if (curdata != data.d) {
//result of you web method is stored in data.d object.
//TODO : Work with you data here.
alert(data.d);
}
}
}
});
}
callService();
</script>
</body>
</html>