如何在没有页面刷新的情况下获取laravel数据?

时间:2016-08-10 06:40:58

标签: javascript php jquery ajax

目前正在通过laravel为实时聊天模块工作。我可以通过ajax将用户聊天数据发送到数据库,但是无法刷新而无法显示数据库中的数据。

以下是显示来自数据库的消息的地方 -

        <ul class="convo-list convo-body">
      <li>
        <div class="row convo-row-msg">
          <div class="col-xs-2 convo-row-msg-avatar">
              <a href="{{ url('/') }}/images/users/photos/profiles/{{ $users->profilephoto }}">  
                <img src="{{url('/') }}/images/users/photos/profiles/{{ $users->profilephoto }}" alt="" width="220px" style="border: 4px solid #fff;border-radius:2px;">
              </a>
          </div>  
          <div class="col-xs-10 convo-row-body">
            <b><a href="#"> Username place here </a> </b>
            <span class="timestamp">
              Timestamp
            </span>
            <p class="convo-content"> User chat message will be there </p>
          </div>
        </div>          
      </li>
    </ul>

这是我需要从数据库中获取的ajax -

var takeHash = $('#hash').val();
var interval = setInterval(function(){

$.ajax({  
    type: 'GET',  
    url: takeHash, 
    success: function(response) {

        $('.convo-body').append(response);

    }
});

}, 1000);

最后我的控制器响应就像那样 -

    public function showSingleMessage($hash)
{
    $msg = Message::where('conversationhash', $hash)
                    ->where('fromid', '!=', Auth::user()->id)
                    ->get();
    $fromid = '';
    foreach ($msg as $value) {            
        $fromid = $value->fromid;
    }

    $messages = Message::where('conversationhash', $hash)
        ->Where('fromid', Auth::user()->id)
        ->orWhere('fromid', $fromid)
        ->get();

    $user = new User();
    return view('messages.show-single')
        ->with(['messages' => $messages, 'user' => $user, 'touser' => $fromid]);
}

那么,任何人都可以帮助我如何在刀片模板中显示我的数据(laravel使用那个)吗?

&#13;
&#13;
var takeHash = $('#hash').val();
var interval = setInterval(function(){

$.ajax({  
    type: 'GET',  
    url: takeHash, 
    success: function(response) {

        $('.convo-body').append(response);

    }
});

}, 1000);
&#13;
<ul class="convo-list convo-body">
      <li>
        <div class="row convo-row-msg">
          <div class="col-xs-2 convo-row-msg-avatar">
              <a href="{{ url('/') }}/images/users/photos/profiles/{{ $users->profilephoto }}">  
                <img src="{{url('/') }}/images/users/photos/profiles/{{ $users->profilephoto }}" alt="" width="220px" style="border: 4px solid #fff;border-radius:2px;">
              </a>
          </div>  
          <div class="col-xs-10 convo-row-body">
            <b><a href="#"> Username place here </a> </b>
            <span class="timestamp">
              Timestamp
            </span>
            <p class="convo-content"> User chat message will be there </p>
          </div>
        </div>          
      </li>
    </ul>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我对输出数据不一致,所以我刚刚在用户界面更新了附加格式。

$.ajax({  
type: 'GET',  
url: takeHash, 
success: function(response) {
 appenddata='<li>';
    if (typeof(response)=='array') {
     appenddata+=response['message'];
   }
  if ( typeof(response)=='object') {
     appenddata+=response->message;
   }
   if ( typeof(response)=='string') {
     appenddata+=response;
   }
     appenddata+='</li>';
    $('.convo-body').append(appenddata);

}
});

}, 1000);