ajax响应创建laravel删除按钮

时间:2018-11-20 08:33:46

标签: javascript ajax laravel

我有这个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控制器中的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;
        }

    }

1 个答案:

答案 0 :(得分:1)

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