C# Don';我不知道如何在聊天应用程序中使用此Ajax代码

C# Don';我不知道如何在聊天应用程序中使用此Ajax代码,c#,ajax,jquery,asp.net-ajax,chat,C#,Ajax,Jquery,Asp.net Ajax,Chat,这是与我的聊天应用程序一起使用的Ajax代码。 我已经成功地完成了聊天应用程序,但我需要使用ajax,这样应用程序就可以较少地访问数据库,但我不知道如何在我的简单聊天应用程序中使用此代码。请有人详细解释代码的用法 <!DOCTYPE HTML> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- Reference to google J-Query api. You can d

这是与我的聊天应用程序一起使用的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>

函数callService(){
//您的web服务的url
$.ajax(“../sessionOut.asmx/GetNewsAndAlerts”,
{
beforeSend:function(xhr){},
完成:函数(){},
contentType:'application/json;charset=utf-8',
数据类型:“json”,
jsonp:“回调”,
键入:“POST”,
错误:函数(xhr、ajaxOptions、thrownError){
//函数将在发生任何错误时调用。
alet(thrownError);
},
成功:功能(数据){
如果(!data.d){
//找不到数据。
}
否则{
if(curdata!=data.d){
//web方法的结果存储在data.d对象中。
//TODO:在这里使用您的数据。
警报(数据d);
}
}
}
});
}
callService();

Ajax背后的思想是减少web应用程序的整页刷新次数。它不会像您在问题中提到的那样减少数据库活动


在本例中,CallService调用一个页面,在本例中为GetNews AndAlerts,当页面结果返回到Ajax调用时,会调用success:代码,此时服务器上的页面返回的任何数据都可供浏览器中网页上的jquery使用。

谢谢您的时间,您说得对,我使用meta标记每秒刷新一次我的页面,这就是为什么我需要使用Ajax来减少刷新次数。我想了解的是,这段Ajax代码如何在不刷新整个页面的情况下显示新的聊天信息,我在哪里编写这段Ajax代码????在Aspx页面中,或在Aspx.cs页面中,或在.asmx页面中,或在某个单独的页面中?Ajax代码始终在web页面客户端上调用,因此在document ready函数中,您可以调用callService代码来加载数据,也可以在处理单击按钮的函数中调用,这取决于您