默认用ajax获取列表
$(document).ready(function() {
  get_pm(1);
});// 获取信息
function get_pm(page)
{   
    var list = '';
    $.get(MODULE_URL + 'getpm', {"touid":touid, "page":page}, function(response){
        $.each(response.data, function(key, rows) {
            list += rows['content'];
        });
        $(".weibo_list").html(list);
}// 表单提交函数
function save()
{
    var fromuid = $('#fromuid).val(); 
    var msgto = $('#msgtoid').val();
    var content = $('#content').val();
    var replyid = $('#replyid').val();
    var image = $('#image').val();
    $.post(MODULE_URL + 'dosend', {"fromuid":fromuid, "msgto":msgto,"replyid":replyid, "content":content, "image":image}, function(response){
        if (response.error_code > 0)
        {
            alert(response.msg+'('+response.error_code+')');
        }
        else
        {    
            $('.send_success').show();
            get_pm(1);
            setTimeout(hide_success, 2000);
        }
    }, 'json');
}
 
// 隐藏发送成功div
function hide_success()
{
    $('.send_success').hide();
    // location.reload(); 
}现在问题是:
我表单提交成功时调用了get_pm(1);应该是重新请求了数据,
表单提交成功了,数据也加到数据库了,但是新的数据无法显示出来,必须刷新下页面才行,不知道是怎么回事?
是ajax哪里用错了吗?

解决方案 »

  1.   

    你用firebug的“网络”就能看出请求和响应的完整过程。包括请求是否发出,参数是什么,返回的数据是什么。
      

  2.   

    MODULE_URL + 'dosend 是如何写的?
      

  3.   


     public function dosend()
        {
            $fromuid = $this->input->post('fromuid');
            $msgto = $this->input->post('msgto');
            $replyid = $this->input->post('replyid');
            $content = $this->input->post('content');
            $image = $this->input->post('image');
     
            $response = $this->message_model->send($fromuid, $msgto, $replyid, $content, $image);
     
            echo json_encode($response);
        }message_model里的send方法
       public function send($fromuid, $msgto, $replypid = 0, & $content, & $image)
        {
            // 参数过滤
            $fromuid = intval($fromuid);
            $msgto = intval($msgto);
            $replypid = intval($replypid);
            $content = html_escape(trim($content));
     
            if ($content == '')
            {
                return array('error_code' => 40020, 'msg' => $this->lang->line('bbs_message_content_null'));
            }
     
            $this->member->load_ucenter();
     
            $return = uc_pm_send($fromuid, $msgto, '', $content, 1, $replypid);
     
            if ($return > 0)
            {
                if ($image != '')
                {
                    $image = $this->copy_image($return, $fromuid, $image, TRUE);
                    if (empty($image))
                    {
                        return array('error_code' => 40006, 'msg' => $this->lang->line('bbs_upload_image_failed'));
                    }
                }
     
                // 更新会员的新私信数
                $this->load->model('bbs/timeline_model');
                $this->timeline_model->update_new_private($msgto);
     
                return array('error_code' => 0, 'msg' => 'ok');
            }
     
            switch($return)
            {
                case 0:
                    $error_code = 40021;
                    $msg = $this->lang->line('bbs_message_send_failed');
                    break;
                case -1:
                    $error_code = 40022;
                    $msg = $this->lang->line('bbs_message_over_user_num');
                    break;
                case -8:
                    $error_code = 40023;
                    $msg = $this->lang->line('bbs_message_send_to_self');
                    break;
                case -9:
                    $error_code = 40024;
                    $msg = $this->lang->line('bbs_message_to_null');
                    break;
            }
     
            return array('error_code' => $error_code, 'msg' => $msg);
        }
      

  4.   

      public function getpm()
        {
            $touid = $this->input->get('touid');
            $page = $this->input->get('page');
     
            $response = $this->message_model->getpm($this->member->uid, $touid, $page);
     
            echo json_encode($response);
        }message_model下的getpm方法: 
        public function getpm($uid, $touid, $page = 1)
        {
            // 参数过滤
            $uid = intval($uid);
            $touid = intval($touid);
            $page = max(1, intval($page));
     
            // 分页数
            $page_size = $this->config->item('bbs_message_page_size');
     
            $this->member->load_ucenter();
     
            // 总数
            $total = uc_pm_view_num($uid, $touid, FALSE);
     
            $data = uc_pm_view($uid, 0, $touid, 5, $page, $page_size, 0, 0);
            $user = array();
            foreach ($data as $key => $rows)
            {
                $user[] = $rows['author'];
                $rows['sendtime'] = date('m月d日 H:i:s', $rows['dateline']);
                $rows['image'] = $this->get_image($rows['pmid']);
                $rows['isfirst'] = 0;
                $data[$key] = $rows;
            }
     
            if ( ! empty($user))
            {
                $user = array_unique($user);
                $user = $this->member->get_basic_by_username($user);
            }
     
            $user_data = $this->member->get($touid);
            $msgto = $user_data['nickname'] . '(' . $user_data['username'] . ')';
     
            return array('error_code' => 0, 'msg' => 'ok', 'total' => $total, 'page_size' => $page_size, 'data' => $data, 'user' => $user, 'msgto' => $msgto);
     
        }
      

  5.   

    我只要确认一下数据是否正确你在
      $('.send_success').show();
      get_pm(1);
    之前
    alert(response.msg);
    看看是什么情况,用于判断问题的所在
      

  6.   


    好像我没把问题描述清楚,起始就和微博首页一样,开始用ajax函数get_pm获取列表,当发布一条新的微博时,再用get_pm的ajax请求一次,把最新的一条微博展现出来,现在发的都是没问题,数据也加到数据库中了,但就是发布成功时,最新的一条信息没有展现出来,要刷新下才能展现出来
      

  7.   

    你有
    $(document).ready(function() {
      get_pm(1);
    });
    刷新当然可以,但这就不是ajax了ajax提交的回调函数中有 get_pm(1);
    按理应该有效的,但我怀疑并没有执行到
      

  8.   

    新手,看不懂,没见过这种ajax格式
      

  9.   

    是否有主从问题导致的同步慢问题?
    Ajax提交评论并接受应答后再次请求刷新还是?