gpt4 book ai didi

javascript - ajax响应创建laravel删除按钮

转载 作者:行者123 更新时间:2023-12-03 00:42:34 25 4
gpt4 key购买 nike

我有这个ajax响应:

$.ajax({
method: 'POST',
url: '/admin/lessons/addMember/licenseMemberId',
data: {'licenseMemberId' : id},
success: function(response){

if ($.trim(response)) {

$('#no_members').hide();
var div1 = document.createElement('div');
div1.setAttribute('class','table-responsive');
$('#space').append(div1);
var actualMembers = document.createElement('table');
actualMembers.setAttribute('class','table');
div1.append(actualMembers);


var newRow = actualMembers.insertRow(actualMembers.length);
id = newRow.insertCell(0);
id.innerHTML = response['user_saved']['id'];
firstname = newRow.insertCell(2);
firstname.innerHTML = response['user_saved']['firstname'];
lastname = newRow.insertCell(3);
lastname.innerHTML = response['user_saved']['lastname'];

id = newRow.insertCell(7);
var llmId=response['llm']['id'];
id.innerHTML = "<form class=delete action='{{ route('lessons.removeMember', ['licenseMemberId' => '" + llmId + "']) }}' method='POST'><input type='hidden' name='_token' value='<?php echo csrf_token(); ?>'><input type='hidden' name='method' value='DELETE'><button class='btn btn-danger btn-xs btn-delete' > <i class='fa fa-trash-o' title='{{__('lesson.remove_member_from_lesson')}}'></i> </button> </form>";

$('#membersModal').modal('hide');

},
error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});

在此方法中,我创建一个新表,并为我在模态中选择的每个元素放置一个新行。问题是删除表单和按钮(id var),我正确创建了它,但是当我单击此按钮时,laravel 返回了 MethodNotAllowedHttpException。我想我必须以其他方式发送 DELETE 方法,但我不知道如何。

这是 Laravel Controller 中的 addMember 方法:

public function addMember(Request $request)
{

$lessonLicenseMember=new LessonLicenseMember();
$lessonId = $request->session()->get('lessonId',1);
$maxMembers=Lesson::find($lessonId)->course->type->max_members;
$actualMembers=$lessonLicenseMember::where('lesson_id','=',$lessonId)->count();


if((!($lessonLicenseMember::where('lesson_id','=',$lessonId)
->where('license_member_id','=',$request->licenseMemberId)
->exists())) && $actualMembers<$maxMembers) {

$lessonLicenseMember->lesson_id = $lessonId;
$lessonLicenseMember->license_member_id = $request->licenseMemberId;
$lessonLicenseMember->save();

$member=LicenseMember::find($request->licenseMemberId)->member;


return response()->json([ 'user_saved' => $member ,'llm'=>$lessonLicenseMember,'actualMembers'=>$actualMembers]);
}
else{
return null;
}

}

最佳答案

如果方法是 POST,请检查您的路由,因为如果方法不匹配,您只会收到 MethodNotAllowedHttpException 错误。

关于javascript - ajax响应创建laravel删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53388996/

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