Laravel与Ajax请求有时获得成功有时没有

时间:2019-01-02 14:42:29

标签: jquery json ajax laravel

我是一名初级程序员,我的代码有一些问题。当我在laravel 5.7中使用Ajax调用数据时,有时成功并获得了数据。。但是当我再次尝试刷新时,有时不完整,并且在日志中错误500是我的php。当我再次尝试刷新数据时,成功了。有人可以帮助我吗?谢谢

这是示例错误日志 example error on my log

并再次刷新,日志工作正常 enter image description here

.. 这是我认为的代码

例如其中一个

  {{-- GROUP CHAT FRIEND --}}

@auth     

  load_grup();
  //
  function load_grup()
  {

    var gpage = {{ (isset($_GET['gpage'])) ? $_GET['gpage'] : '0' }}

    $.ajax({
        type : 'GET',
        url : "{{ url('json/group_chat?gpage=') }}"+parseInt(gpage),
        success : function (data) {


          var options = "";
          var arsip = "";
          for (var i = 0; i < data.length; i++) {


            if(data[i].status == '1')
            {
              status = '<span class="ml-auto badge badge-success">Online</span>';
            }
            else
            {
              status = '<span class="ml-auto badge badge-secondary">Offline</span>';
            }

            options += `

            <tr class="`;
            if(data[i].count > 0)
            {
              options += 'bg-secondary';
            }
            options +=`">
              <td> </td>
              <td>
               <a href="{{url('chat/group')}}/`+data[i].room_id+`" class="font-weight-bold text-dark">  `+ data[i].name +` </a> <span class="badge badge-danger"> `+data[i].count+` </span> <br>
               <a href="{{url('chat/group')}}/`+data[i].room_id+`" class="text-dark">
                `;

                if(data[i].body != null)
                {

                  options += ``+data[i].body+``;
                }

            options +=`
                </a> <br>

                `+data[i].time+`
              </td>
            </tr>

            `;


            arsip += `
            <li class="list-group-item `;

            if(data[i].count > 0)
            {
              arsip += 'bg-secondary';
            }
            arsip += `">
              <div class="row">
                <div class="col-md-2">
                </div>
                <div class="col-md-10">
                  <span class="font-weight-bold">
                    <a href="{{url('chat/group')}}/`+data[i].room_id+`" class="text-dark"> `+data[i].name+` </a>
                    <span class="badge badge-danger">`+data[i].count+`</span>

                    <div class="row">
                      <label for="" class="ml-auto mr-3">`+data[i].time+`</label>
                    </div>
                  </span>
                  <article class="article">
                  <a href="{{url('chat/group')}}/`+data[i].room_id+`" class="text-dark">  `;

                  if(data[i].body != null)
                  {

                    arsip += ``+data[i].body+``;
                  }

                  arsip += ` </a>
                  </article>
                </div>
              </div>

            </li>
            `;
          }

          if(data[0].pagination != 0)
          {
            $("#group_chat-list").html(options);
            $("#arsip-group_chat").html(arsip);
          }
            // CREATE PAGINATION

            var before_page = {{(isset($_GET['gpage']) ? $_GET['gpage'] : '0' )}};

            var page_chat = ``;

            page_chat+=`<a `;

            var be_page = parseInt(before_page);

            if(be_page == 0)
            {

              page_chat +=`href="{{url('chat/group')}}" class="btn btn-link text-secondary" style="cursor:default;"`;
            }
            else if(be_page > 0)
            {
              page_chat +=`href="{{url('chat/group')}}`+ (parseInt(before_page-1) == 0 ? '' : `?gpage=`+parseInt(before_page-1)) +`" class="btn btn-link `+(parseInt(before_page-1) == 0 ? 'text-secondary' : '')+`"`;

            }

            page_chat += `>Sebelumnya</a>
            <a `;

            var be_page = parseInt(before_page);

            if(parseInt(before_page * 5) >= data[0].pagination)
            {
              page_chat += `class="btn btn-link text-secondary" style="cursor:default;"`;
            }
            else
            {
              page_chat += `class="btn btn-link" href="{{url('chat/group')}}?gpage=`+ (parseInt(before_page+1) == 1 ? 2 : parseInt(before_page+1) ) +`"`;
            }

            page_chat += `>Selanjutnya</a>
            `;

            if(data[0].pagination >= 5)
            {
              $("#page_group").html(page_chat);
            }

          }
    });

  }
</script>

@endauth

然后是我的控制器

public function group_chat(){
$number = 0;
if(isset($_GET['gpage']))
{
  $number = $_GET['gpage']-1;

}
// LIMIT 5
$offset = 5;

// CREATE PAGINATE GROUP
$room_names = User_room::select("users__room.id as id")
->join('users__room_chat_partisipants', 'users__room_chat_partisipants.user_room_id', '=', 'users__room.id')
->leftjoin('users__room_chat', 'users__room_chat.user_room_id', '=','users__room.id')
->leftjoin('users__room_chat_documentation', 'users__room_chat_documentation.user_room_chat_id', '=','users__room_chat.id')
->where('users__room.name', 'not like', '%-%')
->where('users__room_chat_partisipants.user_id', '=', Auth::user()->id)
->orderBy('users__room_chat.id', 'desc')
->groupBy('users__room.id')
->skip($offset * $number)
->take($offset)
->get();


$count_page = User_room::select("users__room.id")
->join('users__room_chat_partisipants', 'users__room_chat_partisipants.user_room_id', '=', 'users__room.id')
->leftjoin('users__room_chat', 'users__room_chat.user_room_id', '=','users__room.id')
->leftjoin('users__room_chat_documentation', 'users__room_chat_documentation.user_room_chat_id', '=','users__room_chat.id')
->where('users__room.name', 'not like', '%-%')
->where('users__room_chat_partisipants.user_id', '=', Auth::user()->id)
->orderBy('users__room_chat.id', 'desc')
->groupBy('users__room.id')
->get();

$x= 0;
foreach($room_names as $field)
{
  $room_fix = User_room_chat::select('*')
  ->where('user_room_id', $field['id'])
  ->orderBy('users__room_chat.id','desc')
  ->first();

  $room_name[$x]['id_chat'] = $room_fix['id'];

  $x++;
}

$room_namex = array();
$k = 0;
rsort($room_name);
foreach($room_name as $chat_id)
{
  $room_fix = User_room::select('users__room_chat.body', 'users__room.id as room_id', 'users__room_chat.created_at', 'users__room.name')
  ->leftjoin('users__room_chat', 'users__room.id', '=', 'users__room_chat.user_room_id')
  ->where('users__room_chat.id', $chat_id['id_chat'])
  ->orderBy('users__room_chat.id','desc')
  ->first();

  $chat_notif = User_room_chat::select("id")
  ->where('user_room_id', $room_fix['room_id'])
  ->where('user_id', '!=', Auth::user()->id)
  ->where('open', '=', '0')
  ->count();

  $room_namex[$k]['body'] = $room_fix['body'];

  $room_namex[$k]['room_id'] = $room_fix['room_id'];

  $room_namex[$k]['time'] = '';

  if($room_fix['created_at'] != null)
  {
    $room_namex[$k]['time'] = $room_fix['created_at']->diffForHumans();
  }

  $room_namex[$k]['name'] = $room_fix['name'];

  $room_namex[$k]['count'] = $chat_notif;

  $k++;
}

$room_namex[0]['pagination'] = COUNT($count_page);

return response()->json($room_namex);
}

1 个答案:

答案 0 :(得分:0)

您可以调试将其添加到ajax函数中的错误:

                    error: function(xhr, error){
                        console.debug(xhr);
                        console.debug(error);
                    }

这将准确显示后端发生的情况。希望对您有帮助。