DragonWar% 2015-02-06 13:33 采纳率: 0%
浏览 36

未定义索引AJAX Laravel

I'm using laravel with json pass my data to controller. Everything seems work fine in html template file. But when comes to controller, $_GET method didn't work at all. Undefined index.

Route

Route::post('/live/{stream_active}/{vid_url}', 'Controller@getAjax');

Controller

public function getAjax($stream_active,$vid_url)
{
    $stream_active = "1";
    $vid_url = $_GET['vid_url']; //Undefine index

    $input = Input::all();
    $full_path = "http://xx.xxx.xx.xx/vod/".$vid_url;

    $input['stream_active'] = $stream_active;
    $input['vid_url'] = $full_path;
    $this->video->create($input);
}

AJAX

$.ajax({
        url : '/live/{stream_active}/{vid_url}',    
        type : 'POST',
        data : { stream_active : '1', vid_url : path_url},
        success : function (data)
        {
            alert('Updated completed.');
        }
});
  • 写回答

1条回答 默认 最新

  • weixin_33743661 2015-02-06 13:37
    关注

    First it doesn't work because you're actually sending a POST request. But also because you have the parameter in your route you would actually use it in the URL when making the call:

    $.ajax({
        url : '/live/1/'+path_url,    
        type : 'POST',
        success : function (data)
        {
            alert('Updated completed.');
        }
    });
    

    And then use the variables that get injected into your controller:

    public function getAjax($stream_active,$vid_url)
    {
        $full_path = "http://xx.xxx.xx.xx/vod/".$vid_url;
    
        $input['stream_active'] = $stream_active;
        $input['vid_url'] = $full_path;
        $this->video->create($input);
    }
    

    The alternative solution would be to remove the parameters from the URL and send the data like you currently do:

    Route::post('/live', 'Controller@getAjax');
    
    $.ajax({
        url : '/live',    
        type : 'POST',
        data : { stream_active : '1', vid_url : path_url},
        success : function (data)
        {
            alert('Updated completed.');
        }
    });
    
    
    public function getAjax()
    {
        $input = Input::all();
        $input['vid_url'] = "http://xx.xxx.xx.xx/vod/" . $input['vid_url'];
        $this->video->create($input);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭