我在一个控制器中有一些功能,例如:
public function cruise($id){...}
public function block($id){...}
在web.php中,我添加了路由:
Route::post('/profile/{cruise}', 'UtilityController@cruise');
Route::post('/profile/{block}', 'UtilityController@block');
在刀片的ajax中:
function block() {
$.ajax({
type: 'POST',
url: "{{route('profile', $user->id)}}", // PROBLEM HERE
dataType: 'JSON',
data: {formData: $data},
success: function ($data) {
console.log('status = ' + status);
},
error: function (json) {
console.log(json);
},
})
}
如何在ajax中从控制器定义特定的功能路由?
问题是您为2种不同的控制器方法使用了相同的路由
Route::post('/profile/{cruise}', 'UtilityController@cruise');
Route::post('/profile/{block}', 'UtilityController@block');
当您编写Route::post('/profile/{cruise}',...
此部分时,{between_curly_braces}
是通配符...。换句话说,这只是laravel分配要发送的值的变量的名称...
在您的情况下$user->id
,无论执行什么操作,都将始终在您的路线文件中找到与该结构相对应的第一条路线post('/profile/{ }
为了使此方法起作用,以便您可以在Controller中使用不同的方法,您必须将路径分开才能使用不同的方法。在路由文件中创建以下两个路由:
Route::post('/profile-cruise/{cruise}', 'UtilityController@cruise')->name('profile.cruise');
Route::post('/profile-block/{block}', 'UtilityController@block')->name('profile.block');
在ajax函数中,按如下所示查找所需的路由:
function cruise() {
$.ajax({
type: 'POST',
url: "{{route('profile.cruise', $user->id)}}",
dataType: 'JSON',
data: {formData: $data},
success: function ($data) {
console.log('status = ' + status);
},
error: function (json) {
console.log(json);
},
})
}
和巡航:
function block() {
$.ajax({
type: 'POST',
url: "{{route('profile.block', $user->id)}}",
dataType: 'JSON',
data: {formData: $data},
success: function ($data) {
console.log('status = ' + status);
},
error: function (json) {
console.log(json);
},
})
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句