javascript - Axios 在创建时返回 405 错误(Laravel+Vue)
问题描述
我有一个“书籍”表单,我正在通过 axios 发送一个 HTTP 请求,它返回 405 错误
我的路线
(web.php)
Route::redirect('/', 'login');
Auth::routes();
Route::resource('/books', 'books\BooksController')->middleware('auth');
我已经看到很多人使用 api 路由,但它以前和我一起使用过 web(但使用不太复杂的 crud)
我的控制器
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 默认的前端脚手架,那可能已经完成了。如果没有,请使用以前的代码。
但请记住,在这两种情况下,您的页面中都需要该元标记<head>
:
<meta name="csrf-token" content="{{ csrf_token() }}">
不相关,但是后面写的所有路由Auth::routes()
,都不需要手动添加auth中间件->middleware('auth')
Auth::routes();
Route::resource('/books', 'books\BooksController');
推荐阅读
- google-chrome-extension - 我如何从 Web 扩展弹出 JavaScript 中知道浏览器是 Chrome 还是 Firefox?
- python - 保存 T-SNE 结果以备将来使用
- django - HTTPConnectionPool(host='0.0.0.0', port=5000): url 超出最大重试次数
- ios - Unity - 按需资源 IOS 13.4
- javascript - chrome.history.deleteUrl 或 chrome.browsingData.remove 的 Google 扩展 HostEquals
- python - Beautiful Soup 一遍又一遍地返回相同的输出
- samba - 使用带有 Time Machine 的 samba 4.9 时出现问题
- c# - 在 httpRequest.Post 中使用没有 SLL 证书的站点
- python - Jira-Python 项目密钥
- java - Spring Batch - 重新触发服务激活器