weixin_33716941 2016-09-21 04:00 采纳率: 0%
浏览 30

Ajax GET请求Laravel 5.2

I have the following in my routes.php:

  Route::get('/displayphotos', function()
  {
    return view('displayphotos')->with('photos', photo::all());
  });

I basically have a javascript code section which initially uses the $photos that the displayphotos routes returns using photo::all()... This does work and everything. I have this running on a 10000 interval.

   setInterval(function() {
      _.forEach({!! $photos !!}, function(value, key) {
        photos_array.push(value.name);
      });

      photos_array = _.uniq(photos_array);

      More code here...

      }, 10000);

So essentially what I want to accomplish is having that $photos get refreshed in here when the interval kicks in it would grab the latest values from $photos which is stored in the database. I am assuming this means I need to make an ajax request somehow using GET? Would I need to add a controller method for this? or can it be done inline somehow?

Can anyone help with how I would accomplish what I am asking?

  • 写回答

1条回答 默认 最新

  • weixin_33724570 2016-09-21 04:17
    关注

    Use an AJAX request because php code executes in the server side and your UI is in the client side, so the operation all runs only once time.

    You can try this:

     Route::get('/displayphotos', function()
     {
       return json_encode(photo::all());
     });
    

    To display a JSON array and use a AJAX request to consume that every interval

    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序