1

I'm using laravel 6. IT returns me error 405 when using external ajax.js file to handle my form. message: The POST method is not supported for this route. Supported methods: GET, HEAD.

This is my form in blade:

<form >
 @csrf
     <div class="form-group">
         <label>Name:</label>
         <input type="text" name="name" class="form-control" placeholder="Name" required="">
      </div>
      <div class="form-group">
          <label>Password:</label>
          <input type="password" name="password" class="form-control" placeholder="Password"       required="">
       </div>
       <div class="form-group">
           <strong>Email:</strong>
           <input type="email" name="email" class="form-control" placeholder="Email" required="">
       </div>
       <div class="form-group">
        <button class="btn btn-success btn-submit">Submit</button>
       </div>
    </form>

my ajax.js:

$(document).on('submit','#employeeSignupFrom',function (e) {
var token =  $('input[name="_token"]').attr('value')
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': token
    }
});
$.ajax({
    $url:'/signupemployee',
    type:'post',
    data: $(this).serialize(),
    contentType:'json',
    success: function( response, textStatus, jQxhr ){
      alert('done')
    },
    error: function( jqXhr, textStatus, errorThrown ){
        alert('error!');
    }
});
e.preventDefault()
 })

the route(web.php):

Route::post('/signupemployee','FormsController@signupEmployee');

and my controller:

<?php

 namespace App\Http\Controllers;

  use Illuminate\Http\Request;
  use Illuminate\Http\Response;
  class FormsControllers extends Controller
   {
    public function signupEmployee(Request $request){
      $employeeInfo=$request->all();
      return response()->json(['alert'=>'done!']);
   }
 }
3
  • clear first your cache by php artisan route:cache Commented Jan 27, 2020 at 7:57
  • it cause an issue!. I can't open any pages.Facade\Ignition\Exceptions\ViewException Route [signupEmployee] not defined. (View: C:\xampp\htdocs\jobyob\resources\views\mainLayout\mLayout.blade.php) Commented Jan 27, 2020 at 8:08
  • what is the issue then ? you are just clearing your cache for you to be sure that you are using the correct route. Commented Jan 27, 2020 at 8:09

1 Answer 1

1

First remove $ sign from your url:

//$url:'/signupemployee', <- remove $
url:'/signupemployee',

Second change contentType to:

contentType: 'application/json',

Finally your ajax should be something like this:

$.ajax({
     url: '/signupemployee', //<- $ sign should deleted
     type: 'POST',
     data: data,
     contentType: 'application/json', //<- not just json
     headers: {
         'X-CSRF-TOKEN': token
     }
})

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

1 Comment

tnx very much, the error 405 was because of my mistake on setting of url in ajax method. I don't know Why I have done this mistake"$url" ! and you are right contentType value is wrong. but my headers works properly @c0mmander

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.