【问题标题】:Send array from Laravel and get object in Vuejs using axios从 Laravel 发送数组并使用 axios 在 Vuejs 中获取对象
【发布时间】:2021-09-27 17:12:28
【问题描述】:

我从 Laravel 发送了一个 array,但在前端我收到了一个 object,不知道为什么。 也许我做错了什么。

//BACKEND
public function index($course_id){
    $courses = CourseTopic::select([
        'course_topics.id',
        'course_topics.name',
    ])
    ->where('course_id', $course_id)
    ->get()
    ->groupBy('id')
    ->toArray();
    
    foreach ($courses as $course_topic) {
      // here I have some logic..
    }
    
    dd(gettype($courses)); -> here is Array
    return response($courses);
}


//FRONTEND
getCourseAreas(){
    this.$http.get('/course-area/' + this.course_id)
    .then(res=>{
        console.log(typeof res.data);  -> here is Object
        this.course_areas = res.data;
    })
},

【问题讨论】:

标签: laravel vue.js axios


【解决方案1】:
  1. 如果你在数组上使用 typeof,它会返回对象
  2. 安全使用 ->json 在您的响应中发送 json 格式的数据到您的前端

【讨论】:

    猜你喜欢
    • 2018-01-04
    • 2021-05-22
    • 2018-02-15
    • 2019-07-26
    • 2019-08-02
    • 2018-03-06
    • 2018-01-30
    • 2022-12-05
    • 2018-07-11
    相关资源
    最近更新 更多