使用AJAX向控制器发送Laravel pass id
路线 javascript使用AJAX向控制器发送Laravel pass id,ajax,laravel,Ajax,Laravel,路线 javascript Route::post('approve', 'PostsController@approve'); 控制器中的方法 $(document).ready(function() { $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $('.btn-approve-post').on('click',
Route::post('approve', 'PostsController@approve');
控制器中的方法
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.btn-approve-post').on('click', function(){
var $btn = $(this);
var post_id = $(this).closest('.post').data('post-id'); // it's a number like 6 or 7 or so on.
$btn.prop('disabled', true);
$.ajax({
type: 'post',
url: 'approve',
data: {'id' : post_id},
dataType: 'json',
success: function(response){
$btn.prop('disabled', false);
console.log(111111);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
});
});
但当我试图通过这种方式传递id时,它不起作用。如何让它工作?在这种情况下,什么类型的响应应该返回我的方法?Laravel中控制器函数中的参数是url中的参数,在路由中定义。要获取post数据,需要使用
$request
变量
您的代码应该是这样的:
class PostsController extends Controller {
public function approve($id)
{
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
}
有关请求变量的更多信息,请参见。Laravel中控制器函数中的参数是url中的参数,在路由中定义。要获取post数据,需要使用
$request
变量
您的代码应该是这样的:
class PostsController extends Controller {
public function approve($id)
{
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
}
有关请求变量的更多信息,请参见。您没有在路由中将
id
用作通配符参数。因此,请尝试以下代码:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller {
public function approve(Request $request)
{
DB::table('posts')
->where('id', $request->id)
->update(['is_approved' => 1]);
}
}
您没有在路由中将
id
用作通配符参数。因此,请尝试以下代码:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller {
public function approve(Request $request)
{
DB::table('posts')
->where('id', $request->id)
->update(['is_approved' => 1]);
}
}
在控制器函数中获取post参数的方法有很多,如下所示: 方法1 将id用作路线中的通配符参数
use Illuminate\Http\Request;
class PostsController extends Controller {
public function approve(Request $request)
{
$id = $request->get('id');
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
}
在ajax函数中,您可以通过以下方式获得:
Route::post('approve/{id}', 'PostsController@approve');
在控制器功能中:
$.ajax({
type: 'post',
url: 'approve/'+post_id,
dataType: 'json',
success: function(response){
$btn.prop('disabled', false);
console.log(111111);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
方法2使用请求方法:
途中:
public function approve($id)
{
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
在Ajax调用中:
Route::post('approve', 'PostsController@approve');
在控制器中:
$.ajax({
type: 'post',
url: 'approve',
data: {'id' : post_id},
dataType: 'json',
success: function(response){
$btn.prop('disabled', false);
console.log(111111);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
在控制器函数中获取post参数的方法有很多,如下所示: 方法1 将id用作路线中的通配符参数
use Illuminate\Http\Request;
class PostsController extends Controller {
public function approve(Request $request)
{
$id = $request->get('id');
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
}
在ajax函数中,您可以通过以下方式获得:
Route::post('approve/{id}', 'PostsController@approve');
在控制器功能中:
$.ajax({
type: 'post',
url: 'approve/'+post_id,
dataType: 'json',
success: function(response){
$btn.prop('disabled', false);
console.log(111111);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
方法2使用请求方法:
途中:
public function approve($id)
{
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
在Ajax调用中:
Route::post('approve', 'PostsController@approve');
在控制器中:
$.ajax({
type: 'post',
url: 'approve',
data: {'id' : post_id},
dataType: 'json',
success: function(response){
$btn.prop('disabled', false);
console.log(111111);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
我尝试过这个,但我得到错误
Class-App\Http\Controllers\Request不存在
adduse-illumed\Http\Request代码>在上课之前。我已编辑了答案。我已尝试此操作,但出现错误Class-App\Http\Controllers\Request不存在
adduse-illusted\Http\Request代码>在上课之前。我已编辑了答案。我已尝试此操作,但出现错误Class-App\Http\Controllers\Request不存在
您应该在命名空间防御下包含以下权限:use-illumb\Http\Request代码>。我还更新了我的答案。我尝试了这个,但我得到错误Class-App\Http\Controllers\Request不存在
您应该在名称空间防御下包括以下权利:使用照明\Http\Request代码>。我也更新了我的答案。