【问题标题】:Undefined index AJAX Laravel未定义索引 AJAX Laravel
【发布时间】:2015-04-06 15:57:38
【问题描述】:

我正在使用带有 json 的 laravel 将我的数据传递给控制器​​。 html 模板文件中的一切似乎都正常。但是当涉及到控制器时, $_GET 方法根本不起作用。未定义的索引。

路线

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

控制器

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.');
        }
});

【问题讨论】:

    标签: php ajax laravel controller


    【解决方案1】:

    首先它不起作用,因为您实际上是在发送 POST 请求。但也因为您在路由中有参数,所以在进行调用时实际上会在 URL 中使用它:

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

    然后使用注入控制器的变量:

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

    另一种解决方案是从 URL 中删除参数并像您当前一样发送数据:

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

    【讨论】:

    • 对于替代解决方案,它不会与我的完整路径连接,而不是单独连接 $vid_url。 @lukasgeiter
    • 哦,我错过了分配完整路径。看看更新的答案。
    猜你喜欢
    • 2018-08-02
    • 2012-08-24
    • 2015-03-23
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多