gpt4 book ai didi

php - 使用 ajax 通过 Laravel Controller 传递数据

转载 作者:行者123 更新时间:2023-12-01 04:39:34 25 4
gpt4 key购买 nike

我有两条路线定义如下:

Route::get('directeur/qualite', 'Directeur\qualiteController@index')->name('qualite');
Route::get('directeur/qualite/{texte}','Directeur\qualiteController@getResultOfQuestion')->name('getResultQuestion');

我的 Controller 有这个功能:

public function getResultOfQuestion(){
$texte=Input::get('texte');
$data = DB::table('questions')->where('texte','=',$texte)->value('code');
return['data'=>$data];
}

我正在使用 Ajax 进行请求,如下所示:

$.ajax({
type: 'GET',
url: '/emagine/projet1613/public/directeur/qualite/',
data: {
texte: encodeURIComponent(str)
},
success: function (data) {
console.log(data);
},
error: function () {
alert('La requête n\'a pas abouti');
}
});

我想获得 Controller 中定义的函数的结果,但我做不到。我做错了什么?

最佳答案

试试这个

Controller

public function getResultOfQuestion($texte){
$data = DB::table('questions')->where('texte','=',$texte)->value('code');
return response()->json(array('data' => $data));
}

AJAX 请求

$.ajax({
type: 'GET',
url: '/emagine/projet1613/public/directeur/qualite/'+encodeURIComponent(str),
success: function (data) {
console.log(data);
},
error: function () {
alert('La requête n\'a pas abouti');
}
});

关于php - 使用 ajax 通过 Laravel Controller 传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41762101/

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