【发布时间】:2018-07-19 11:53:17
【问题描述】:
我正在尝试使用 axios 获取记录。 但问题是我在控制台中收到错误,如下所示: GET http://127.0.0.1:8000/getData500(内部服务器错误)
这是我试图获取数据的组件
<template lang="html">
<div class="getData">
<h1 class="text-center text-muted">Recently Added Chemicals</h1>
<div class="table-responsive">
<table class="table table-striped">
<tr>
<th>Chemical Name</th>
<th>Status</th>
<th>Stock</th>
</tr>
<tr v-for="list in lists">
<td>{{list.chem_name}}</td>
<td>
<p v-if="lists.is_active = 1" style="color: green;">Active</p>
<p v-else style="color: red;">Not Active</p>
</td>
<td>
<p v-if="lists.is_stocked = 1" style="color: green;">In Stock</p>
<p v-else style="color: red;">Out Of Stock</p>
</td>
</tr>
</table>
</div>
</div>
</template>
<script>
export default {
data () {
return {
lists: {},
errors: {}
}
},
mounted(){
axios.get('getData').then((response) => this.lists = response.data).catch((error) => console.log(this.error));
}
}
</script>
然后是我在资源控制器中的getData函数
public function getData(){
$id = Auth::user()->id;
$data = chemType::where('user_id', $id)->orderBy('id', 'DESC')->get();
return response()->json(200, $data);
}
请帮忙!!!
【问题讨论】:
-
请在网络选项卡中查看您的开发人员工具,您应该会看到获取请求旁边有状态 500 错误。如果可能,单击它,然后单击“响应”以获取完整的错误消息。
-
“响应”选项卡显示“已成功添加”。我正在尝试添加一个文本字段,并且在同一个组件上我还添加了另一个子组件以显示最新记录而无需重新加载页面。
-
如果你只是
return $data会发生什么,因为 Laravel 会自动返回一个 json 响应。 -
如果我只是
return $dataapp.js:50923 Uncaught (in promise) TypeError: Cannot read property 'data' of undefined at app.js:50923 at -
好的,所以没有 500 错误?否则,如果有错误,它将被捕获。您确定您的查询实际上正在返回某些内容吗?
标签: vue.js vuejs2 vue-component axios vue-resource