0

Them I click the subscribe button it gives me error messages below

MethodNotAllowedHttpException

but I already define the method in the route file

Route::post('/emailsub','EmailSubController@emailsub');

The Controller Part are here

public function emailsub(Request $request){
    $data = new EmailSub;
    $data->email = $request->email;
    $data->save();
    return $request->all();;
}

Here is the Html Code Of my form

 <form method="POST" >
        {{csrf_field()}}
            <input type="text" id="email" placeholder="email" required="">

            <input type="submit" id="subbtn" value="Subscribe">          

     </form>

Here Is the Ajax part

  $('.button').on('click','#subbtn',function(){

           $.ajax({
                  type: 'post',
                  url: "emailsub",
                  data:{
                      'email':$('#email').val(),
                      '_token':$(input['name=_token']),
                  },
                  success: function (data) {
                      $('#subbtn').val("Unsubscribe");
                  },
              });

    });
Sharifur Robin
  • 327
  • 1
  • 3
  • 15
  • Possible duplicate of [laravel throwing MethodNotAllowedHttpException](https://stackoverflow.com/questions/19760585/laravel-throwing-methodnotallowedhttpexception) – Catalyst Jun 18 '17 at 20:32

2 Answers2

0

Change the ajax to:

$.ajax({
    type: 'post',
    url: "/emailsub",
    data:{
        'email':$('#email').val(),
        '_token':$('input["name=_token"]').val(),
    },
    success: function (data) {
        $('#subbtn').val("Unsubscribe");
    },
});

You weren't putting the value of the crsf field in and the url should have a leading slash.

  • It Still Give Me The Same Error – Sharifur Robin Jun 18 '17 at 20:35
  • Try changing the event listener: `$('#subbtn').on('click' ,function(e){`. I don't see a class `button` anywhere in your html. Also add `e.preventDefault()` before you send the ajax off. –  Jun 18 '17 at 20:40
  • One more problem I noticed, missing quotes in the token selector, should be: `'_token':$('input["name=_token"]').val()` –  Jun 18 '17 at 21:04
0

Type: String An alias for method. You should use type if you're using versions of jQuery prior to 1.9.0. Try using method instead of type. method not allowed means it does not see the request as post request.

Lucas Gervas
  • 369
  • 2
  • 5