错误:请求失败,状态码为405 [英] Error: Request failed with status code 405

查看:114
本文介绍了错误:请求失败,状态码为405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在laravel react.js组合中建立axios示例.我通过以下命令配置了我的项目作曲者create-project --prefer-dist laravel/laravel react-laravel-basic-8.PHP的工匠预设反应安装npm审核修复--forcenpm安装axiosnpm run devPHP工匠服务.我的web.php文件是

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');

我的控制器Admin.php是

My controller Admin.php is

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class Admin extends Controller
{

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

我的视图add_student.blade.php是

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>

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

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'));
}

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

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

推荐答案

出现此错误的原因是,您将 POST 请求发送到 GET .

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

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

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

更改此

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

进入

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

这篇关于错误:请求失败,状态码为405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆