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);
    }
    
    评论

报告相同问题?