【发布时间】:2019-10-19 00:56:05
【问题描述】:
我的代码有问题。当我尝试使用 Vue.JS 和 Laravel 存储数据时,我在控制台中收到错误 500。我坚持了 6 个小时。
控制器 - DashboardAdvantagesController.php:
public function store(Request $request)
{
$request->validate([
'icon'=> 'required',
'title'=> 'required'
]);
$advantage = $request->HomepageAdvantages()->create([
'icon'=> $request->icon,
'title'=> $request->title
]);
return response()->json([
'advantage'=> $advantage,
'message'=> 'task has been created!'
]);
}
型号 - 主页优势
class HomepageAdvantages extends Model
{
protected $fillable = [
'icon', 'title', 'text',
];
}
API 路由
Route::post('/advantages/store', 'DashboardAdvantagesController@store');
Vue 组件
createAdvantage(){
console.log(this.advantage.icon);
console.log(this.advantage.title);
axios.post('http://127.0.0.1:8000/api/advantages/store', {icon: this.advantage.icon,
title: this.advantage.title})
.then(response=>{
this.advantages.push(response.data.advantage);
})
.catch(error=>{
console.log(error);
});
console.log(response.data.advantage);
},
我的错误:
加载资源失败:服务器响应状态为 500 (内部服务器错误)
:8000/api/advantages/store:1 Failed to load resource: the server responded with a status of 500 (Internal Server Error)
2app.js:267 POST http://127.0.0.1:8000/api/advantages/store 500 (Internal Server Error)
dispatchXhrRequest @ app.js:267
xhrAdapter @ app.js:118
dispatchRequest @ app.js:706
Promise.then (async)
request @ app.js:513
Axios.<computed> @ app.js:533
wrap @ app.js:966
createTask @ app.js:1773
invokeWithErrorHandling @ app.js:9448
invoker @ app.js:9773
original._wrapper @ app.js:15126
【问题讨论】:
-
遇到 500 内部服务器错误时,您总是要做的第一件事就是检查服务器的错误日志!
标签: javascript php laravel vue.js