Vue js Laravel axios POST 500 内部服务器错误
Vue js Laravel axios POST 500 Internal Server Error
我刚刚创建了 laravel Vuejs 项目,使用 Axios 进行调用 API。
Get Api 运行良好,但是当我要 post 我的数据时,出现内部服务器错误。
我正在为控制器使用资源。
我只是创建 Api 并通过以下方式获取数据:
axios.get("http://localhost:8080/vuelaravel/tasks")
.then(response =>{this.tasks = response.data})
.catch(error => console.log(error.response));
vuelaravel 是我的项目名称。
它运作良好。但是当我尝试将 post 数据存入我的数据库时,它显示了 500 个内部错误。
Here is my controller:
<?php
namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class TodoController extends Controller
{
public function index()
{
$tasks=Todo::orderBy('id', 'desc')->paginate(2);
return request()->json(200,$tasks);
}
public function create()
{
//
}
public function store(Request $request)
{
$todo=Todo::create($request->all());
if($todo){
$tasks=Todo::orderBy('id', 'desc')->paginate(2);
return request()->json(200,$tasks);
}
}
public function show(Todo $todo)
{
//
}
public function edit(Todo $todo)
{
//
}
public function update(Request $request, Todo $todo)
{
//
}
public function destroy(Todo $todo)
{
//
}
}
还有我的ADD.Vue
<template>
<div>
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="text" name="name" v-model="name" id="name" placeholder="Title Name" class="form-control" >
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" @click="addRecord">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return{
name:'',
}
},
methods:{
addRecord(){
axios.post("http://localhost:8080/vuelaravel/tasks",{
'name': this.name,
})
.then(data => console.log(data))
.catch(error => console.log(error))
}
}
}
</script>
<style scoped>
</style>
这是错误:
app.js:285 POST http://localhost:8080/vuelaravel/tasks 500 (Internal Server Error)
at createError (app.js:699)
at settle (app.js:960)
at XMLHttpRequest.handleLoad (app.js:168)
我在 Welcome.blade.php
中使用我的 csrf
<meta name="csrf-token" content="{{csrf_token()}}">
通过此获取我的数据 Api:
created(){
axios.get("http://localhost:8080/vuelaravel/tasks")
.then(response =>{this.tasks = response.data})
.catch(error => console.log(error.response));
}
试试这个
response($tasks)
方法
$tasks=Todo::orderBy('id', 'desc')->paginate(2);
return response($tasks);
由于您在模型中使用了创建方法,请确保通过将此代码放在模型中来分配质量。
protected $guarded = [];
请阅读此文档soruce
我刚刚创建了 laravel Vuejs 项目,使用 Axios 进行调用 API。
Get Api 运行良好,但是当我要 post 我的数据时,出现内部服务器错误。
我正在为控制器使用资源。
我只是创建 Api 并通过以下方式获取数据:
axios.get("http://localhost:8080/vuelaravel/tasks")
.then(response =>{this.tasks = response.data})
.catch(error => console.log(error.response));
vuelaravel 是我的项目名称。
它运作良好。但是当我尝试将 post 数据存入我的数据库时,它显示了 500 个内部错误。
Here is my controller:
<?php
namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class TodoController extends Controller
{
public function index()
{
$tasks=Todo::orderBy('id', 'desc')->paginate(2);
return request()->json(200,$tasks);
}
public function create()
{
//
}
public function store(Request $request)
{
$todo=Todo::create($request->all());
if($todo){
$tasks=Todo::orderBy('id', 'desc')->paginate(2);
return request()->json(200,$tasks);
}
}
public function show(Todo $todo)
{
//
}
public function edit(Todo $todo)
{
//
}
public function update(Request $request, Todo $todo)
{
//
}
public function destroy(Todo $todo)
{
//
}
}
还有我的ADD.Vue
<template>
<div>
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="text" name="name" v-model="name" id="name" placeholder="Title Name" class="form-control" >
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" @click="addRecord">Save changes</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
data(){
return{
name:'',
}
},
methods:{
addRecord(){
axios.post("http://localhost:8080/vuelaravel/tasks",{
'name': this.name,
})
.then(data => console.log(data))
.catch(error => console.log(error))
}
}
}
</script>
<style scoped>
</style>
这是错误:
app.js:285 POST http://localhost:8080/vuelaravel/tasks 500 (Internal Server Error)
at createError (app.js:699)
at settle (app.js:960)
at XMLHttpRequest.handleLoad (app.js:168)
我在 Welcome.blade.php
中使用我的 csrf <meta name="csrf-token" content="{{csrf_token()}}">
通过此获取我的数据 Api:
created(){
axios.get("http://localhost:8080/vuelaravel/tasks")
.then(response =>{this.tasks = response.data})
.catch(error => console.log(error.response));
}
试试这个
response($tasks)
方法
$tasks=Todo::orderBy('id', 'desc')->paginate(2);
return response($tasks);
由于您在模型中使用了创建方法,请确保通过将此代码放在模型中来分配质量。
protected $guarded = [];
请阅读此文档soruce