Error: Request failed with status code 405

Viewed 12860

I am trying to establish axios sample in laravel react.js combination.I configured my project by following commands composer create-project --prefer-dist laravel/laravel react-laravel-basic-8. php artisan preset react npm install npm audit fix --force npm install axios npm run dev php artisan serve. My web.php file is

Route::get('/', function () {
    return view('welcome');
});
Route::get('add-a-student',function()
{
    return view('add_student');
});
Route::get('/testing/','Admin@testing');

My controller Admin.php is

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Admin extends Controller
{

    public function testing()
    {
        echo "hello alert";
    }
}

And my view add_student.blade.php is

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <title>React axios</title>
    <link rel="stylesheet" href="{{ asset('css/app.css') }}">
    </head>
    <body>
        <div id="basic"></div>
        <script src="{{ asset('js/app.js') }}">
        </script>
    </body>
</html>

And my jsx file resides in resources/assets/js/components/Example.jsx i.e

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';

export default class Example extends Component {
    render() {
        return (
            <div className="container">
                <div className="row">
                    <div className="col-md-8 col-md-offset-2">
                        <div className="panel panel-default">
                            <div className="panel-heading">Example Component</div>

                            <div className="panel-body">
                                I'm an example component!
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}
class Developer extends React.Component{
    handlesubmit(e)
    {
        e.preventDefault();
        axios.post('/testing/', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    alert('success');
  })
  .catch(function (error) {
    alert(error);
  });

    }
    render()
    {
        return(<div><form onSubmit={this.handlesubmit}>
            <table>
            <tr>
            <td>
            <label>Date of birth</label>
            </td>
            <td>
            <input type="date" name="dob"  />
            </td>
            </tr>
            <tr><td><button type="submit">Add student</button></td>

             <td><button type="reset">Cancel</button></td>
            </tr>
            </table>
            </form></div>);
    }
}

if (document.getElementById('example')) {
    ReactDOM.render(<Example />, document.getElementById('example'));
}
if(document.getElementById('basic'))
{
    ReactDOM.render(<Developer />,document.getElementById('basic'));
}

I got following error Error: Request failed with status code 405 while trying to submit form via axios method.Plaese help me to fix the bug

1 Answers

The reason you're getting that error is that you are sending POST request to a route that is a GET.

Therefore, you can change your route from GET to POST to be allowed to process POST requests.

Change this

Route::get('/testing/','Admin@testing');

Into this

Route::post('/testing/','Admin@testing');
Related