我有一个“ Books”表单,并且正在通过axios通过post发送HTTP请求,它返回405错误
我的路线
(web.php)
Route::redirect('/', 'login');
Auth::routes();
Route::resource('/books', 'books\BooksController')->middleware('auth');
我已经看到很多人使用api路由,但是它以前曾与我一起使用过web(但使用的是复杂程度较低的类)
我的控制器
public function store(Request $request)
{
$book = new Book();
$book->isbn = $request->isbn;
$book->titulo_ori = $request->titulo_ori;
$book->sinop = $request->sinop;
$book->n_pag = $request->n_pag;
$book->save();
return $book;
}
我没有使用迁移,所以我的模型看起来像这样
我的模特
class Book extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'sjl_libros';
protected $fillable = ['isbn', 'titulo_ori' 'sinop', 'n_pag'];
public $timestamps = false;
}
我的表格
<b-form @submit.prevent="add">
<b-row>
<b-col cols="4">
<label for="isbn">ISBN</label>
<b-form-input type="text" v-model="book.isbn" id="isbn" name="isbn"></b-form-input>
</b-col>
<b-col cols="4">
<label for="titulo_ori">Original Title</label>
<b-form-input type="text" v-model="book.titulo_ori" id="titulo_ori" name="titulo_ori"></b-form-input>
</b-col>
</b-row><br>
<b-row>
<b-col cols="12">
<label for="sinop">Plot</label>
<b-form-textarea v-model="book.sinop" size="lg" rows="8" id="sinop" name="sinop"></b-form-textarea>
</b-col>
</b-row><hr>
<b-row>
<b-col cols="4">
<label for="n_pag">Number of pages</label>
<b-form-input v-model="book.n_pag" id="n_pag" name="n_pag"></b-form-input>
</b-col>
</b-row><hr>
<div class="d-flex flex-row-reverse bd-highlight">
<b-button variant="default" @click="add">Continue</b-button>
<b-link class="btn btn-danger" href="/books">Cancel</b-link>
</div>
</b-form>
我的axios脚本
add() {
const params = {
isbn: this.book.isbn,
titulo_ori: this.book.titulo_ori,
sinop: this.book.titulo_esp,
n_pag: this.book.n_pag,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
};
axios.post('/books', params)
.then(res => {
alert('success');
console.log(res.data);
}). catch (e=> {
console.log(e);
})
},
我在控制台中得到的确切错误如下
Error: "Request failed with status code 405"
createError http://localhost:8000/js/app.js:653
settle http://localhost:8000/js/app.js:899
handleLoad http://localhost:8000/js/app.js:166
我该怎么做才能解决这个问题?
一个资源路线就像你有一个:
Route::resource('books', 'books\BooksController');
将为post方法生成如下结果:
Verb URI Action Route Name
POST /books store books.store
因此,对于axios url
,必须使用'/books'
(路线URI
)
axios.post('/books', params)
另外,您将需要在字段中添加CSRF令牌_token
。您可以将其附加到params
对象
const params = {
isbn: this.book.isbn,
titulo_ori: this.book.titulo_ori,
sinop: this.book.sinop,
n_pag: this.book.n_pag,
_token: document.querySelector('meta[name="csrf-token"]').getAttribute('content')
};
如果以前没有做过标记,则应该仅将标记添加到标头中
window.axios = require('axios');
window.axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' : document.querySelector('meta[name="csrf-token"]').getAttribute('content');
};
但是,如果您使用的是Laravel默认的前端支架,则可能已经完成。如果没有,请使用之前的代码。
但是请记住,在两种情况下,您都需要在页面中使用该meta标签<head>
:
<meta name="csrf-token" content="{{ csrf_token() }}">
无关,但是对于您之后编写的所有路由Auth::routes()
,您无需手动添加auth中间件->middleware('auth')
Auth::routes();
Route::resource('/books', 'books\BooksController');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句