0

I have this ajax response:

$.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);
            }
        });

In this method I create a new table and I put a new line for each element I choose in a modal. The problem is the delete form and button (the id var), I create it correctly but when I click this button laravel returns me a MethodNotAllowedHttpException. I think I have to send the DELETE method in other manner, but I don't know how.

This is the addMember method in laravel controller:

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 Answer 1

1

Check your routes if the method is POST because you will only get the error MethodNotAllowedHttpException if the method does not match.

Sign up to request clarification or add additional context in comments.

3 Comments

The route is correct: DELETE | admin/lessons/removeMember/{licenseMemberId} | lessons.removeMember | App\Http\Controllers\admin\LessonController@removeMember | web
I want i DELETE method, not a POST method
try this <input type='hidden' name='_method' value='DELETE'>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.