gpt4 book ai didi

javascript - 如何将数组从 View 传递到 Controller ?使用 Laravel

转载 作者:行者123 更新时间:2023-11-28 04:19:33 25 4
gpt4 key购买 nike

我几天来一直遇到无法解决的问题。

我的 View 中应该有一个 javascript/ajax/jQuery 函数,它创建一个数组并使用路由“/modulos/contas_ti/gerar_protocolo”将用户发送到新页面

这是我的 JavaScript:

function check() {
// I don't know how many numbers I will have in my array.
// It will be dinamic. Can be [1, 2] or [1, 4, 5, 6] or anything else.
var array = [1, 2];

// I would like to pass 'array' in the URL below as parameter
window.location.href = "{{URL::to('/modulos/contas_ti/gerar_protocolo')}}"
}

我的路线:

// Maybe pass the array at the end of 'gerar_protocolo'? 
// Like 'gerar_protocolo[]=' ?

Route::get('/modulos/contas_ti/gerar_protocolo', 'ContasTI\ContasTIController@gerarProtocolo');

我的 Controller :

// How to pass the array as parameter inside ()? I also need to 
// pass the array to the new view using 'with', right?
// Like with->('datas', $data);

public function gerarProtocolo() {
return view('modulos.contas-ti.gerar_protocolo');
}

最佳答案

您可以将其作为请求参数发送

function check() {
var array = [1, 2];

window.location.href = "{{URL::to('/modulos/contas_ti/gerar_protocolo')}}" + "?array[]=1&array[]=2";
}

Controller :

public function gerarProtocolo(Request $request) {
$data = request('array');
return view('modulos.contas-ti.gerar_protocolo', compact('data'));
}

关于javascript - 如何将数组从 View 传递到 Controller ?使用 Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45548672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com