javascript - 如何在不刷新页面的情况下获取laravel数据?

标签 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);

最后我的 Controller 像这样响应 -

    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]);
}

那么,有人可以帮助我如何在 Blade 模板中显示我的数据(laravel 使用它)吗?

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

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

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

    }
});

}, 1000);
<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({  
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);

关于javascript - 如何在不刷新页面的情况下获取laravel数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38866009/

相关文章:

jquery group 每 3rd div

javascript - 将类添加到同位素中的过滤项目和隐藏项目。按钮不是复选框

javascript - 从代码中填写网站上的表格

php - Javascript 在读取 cakephp 发送的 json 服务器错误响应后说未定义

php - 当密码不正确时,将用户名保留在用户名字段中

javascript - 未捕获的类型错误 : Cannot read property 'addMethod' of undefined

javascript - JavaScript 中的高效方法

php - 在 Twig 中使用自定义函数

php - 错开电子邮件发送

jquery - Visual Studio 2010 中使用 jquery 进行智能感知