【问题标题】:Don't Know How to use This Ajax code in my chat application不知道如何在我的聊天应用程序中使用此 Ajax 代码
【发布时间】:2013-11-11 05:22:17
【问题描述】:

这是用于我的聊天应用程序的 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>

【问题讨论】:

    标签: c# ajax jquery asp.net-ajax chat


    【解决方案1】:

    Ajax 背后的理念是减少 Web 应用程序执行的整页刷新次数。正如您在问题中提到的那样,它不会减少数据库活动。

    在这种情况下 CallService 在这种情况下调用一个页面 getnewsandalerts 并且成功:当页面结果返回到 Ajax 调用时调用代码,此时服务器上页面返回的任何数据都可用于 web 上的 jquery浏览器中的页面。

    【讨论】:

    • 感谢您的宝贵时间,您说得对,我使用 meta 标签每秒刷新我的页面,这就是为什么我需要使用 Ajax 来尽量减少刷新次数
    • 我想了解的是,这个 Ajax 代码如何在不刷新整个页面的情况下附加新的聊天消息
    • 我在哪里写这个 Ajax 代码????在 Aspx 页面中,还是在 Aspx.cs 页面中,或者在 .asmx 页面中,或者在某个单独的页面中?
    • Ajax 代码总是在网页客户端调用,因此在文档就绪函数中,您可以调用 callService 代码来加载数据,它可以在处理单击按钮的函数中,这取决于你
    猜你喜欢
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多