drjun1994 2013-09-19 12:15
浏览 98
已采纳

如何使用jquery ajax向php页面发送异步请求

i am new to web development creating a kind of social networking website for college project. I want to include update the messages count in the message menu every time there is a new msg in the database for the user(like facebook message menu on homepage)

But it's frustrating learning ajax, however after searching on web and reading some topics from some books I came to the solution that i can make an $ajax call in my js file in the homepage and send data ('name'=>'user') stored in javascript cookie that i have created on loading of home page after the user login, to a php file which will search across the recent_msg table in database to fetch the recent message for the logged in user if any after fetching the php file will create the html file with code snippet and further another jquery code will append that snippet from file to the message list menu.

the PHP part is not the problem but how can i send the username to the php file using jquery ajax api, here is the code what i think i can apply but i am doubtful in that if this is the correct way

$(document).ready(function{

setInterval ( function()
{
    var usr = getCookie("name");
    $.ajax ( {
        url: '/phpScripts/recent_msg.php',
        type: 'POST',
        data: usr,
        success: function(data){

             }
        } );
},10);
});

what is the purpose of success function in the code?

  • 写回答

3条回答 默认 最新

  • duandu1966 2013-09-19 12:19
    关注

    data needs to be in the form of an object / key-value-pair (EDIT: or if a string, as a valid querystring). data: { name: usr }. However, since it's in a cookie, your PHP page will have direct access to that cookie. It's safer to let your session cookie tel the PHP page who the user is instead of relying on an AJAX call to tell the PHP page who it is.

    http://php.net/manual/en/features.cookies.php

    So I'd drop data from your AJAX call altogether, and in your PHP page, use $_COOKIE["name:"]

    Then whatever HTML gets passed back from the PHP page will arrive in the data call. If it's HTML, then simply add it to your HTML to some message div, such as.

    <div id="recent-messages"></div>
    
    <script type="text/javascript">
    $(document).ready(function{
    
    setInterval ( function()
    {
        var usr = getCookie("name");
        $.ajax ( {
            url: '/phpScripts/recent_msg.php',
            type: 'POST',
            data: usr,
            success: function(data){
                    $('#recent-messages').html(data);
                 }
            } );
    },10);
    });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示