错误:请求失败,状态码为 405

use*_*298 3 reactjs laravel-5

我正在尝试在 laravel react.js 组合中建立 axios 示例。我按照 composer create-project --prefer-dist laravel/laravel react-laravel-basic-8 命令配置了我的项目。php artisan 预设 react npm install npm audit fix --force npm install axios npm run dev php artisan serve。我的 web.php 文件是

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

我的控制器 Admin.php 是

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Admin extends Controller
{

    public function testing()
    {
        echo "hello alert";
    }
}
Run Code Online (Sandbox Code Playgroud)

我的观点 add_student.blade.php 是

<!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>
Run Code Online (Sandbox Code Playgroud)

我的 jsx 文件驻留在 resources/assets/js/components/Example.jsx 即

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'));
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误错误:尝试通过 axios 方法提交表单时请求失败,状态代码为 405。Plaese 帮我修复错误

Dav*_*ulo 5

您收到该错误的原因是您将POST请求发送到GET路由。

因此,您可以将路由从GET更改为POST以允许处理POST请求。

改变这个

Route::get('/testing/','Admin@testing');
Run Code Online (Sandbox Code Playgroud)

成这个

Route::post('/testing/','Admin@testing');
Run Code Online (Sandbox Code Playgroud)