405(方法不允许) - Laravel和Vue.js



大家下午好,我正在尝试实现一个通知系统,可以用喜欢标记此通知。

我在后端使用 laravel 7,在前端使用 vuejs。

代码在本地主机上正常工作,但是当我部署到 Heroku 时,它停止工作并给我以下消息。

http://springalert.herokuapp.com/api/like 405 (Method Not Allowed)
Uncaught (in promise) Error: Request failed with status code 405
at createError (app.js:5347)
at settle (app.js:5608)
at XMLHttpRequest.handleLoad (app.js:4816)

有人对这个主题有任何提示,我研究了一下,我知道我们必须配置 CORS,但对于这个版本的 laravel,它应该不再需要。

按照代码进行操作,感谢您的帮助。

路线

Route::post('/api/like/', 'NotificationController@api_like');

控制器

public function api_like(Request $request) {
$like = new Like;
$like->notification_id = $request->id;
$like->user_id = auth()->id();
$like->save();
}

维耶斯

<b-card-text class="text-right" v-if="Object.keys(notification.like).length == 0">
<a @click="makelike('success', 'Informação', notification.id)" class="a"><i class="fas fa thumbs-up"></i></a>
</b-card-text>
makelike(variant = null, title, notification_id) {
this.id = notification_id
axios.post('api/like/',{ id:this.id })
.then((response) => {
this.set_notifications()
this.$bvToast.toast('Obrigado pela tua visualização', {
title: title,
variant: variant,
solid: true
})
})
},

正如用户Mihai指出的那样,你需要在get请求之后将你的POST请求链接到csrf。

以下是Laravel Sanctum的外观:

import axios from 'axios'
const baseUrl = 'http://yourdomain.com'
const api = axios.create({
baseURL: baseUrl,
withCredentials: true
})
const payload = {}
api().get('/sanctum/csrf-cookie').then(() => {
return api().post('api/like', payload).then(resp => {
// Do stuff with resp
})
})

最新更新