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 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名