douzhaxian1267 2017-12-20 01:04
浏览 57
已采纳

如何通过laravel与离子连接到数据库

I'm new to Ionic and I'm having trouble getting data from a database in a server.

What I'm trying to do is to connect Ionic to the database in a server through a laravel app.

this.http
  .get('http://127.0.0.1:8000/retrieve_data.php')
  .subscribe((data : any) =>
  {
     console.dir(data);
     this.items = data;
     console.log(this.items);
  },
  (error : any) =>
  {
     console.dir(error);
  });

Where retrieve_data.php is a file that contains the database connection and query, but nothing shows up in my app.

So I tried doing the same thing but with a local host. If I run the retrieve_data.php file directly from my browser it shows

[{"id":1,"name":"Diego Rodriguez","email":"drodriguezm@fastfit.cl","password":"fastfit","remember_token":null}]

Which is the answer from the query to the db in the localhost. But when I try to do the same thing in Ionic like: this.http.get('http://localhost/retrieve_data.php') I get nothing.

Please help :(

  • 写回答

2条回答 默认 最新

  • dongyong6332 2018-01-04 22:01
    关注

    I was doing it wrong, you have to use laravel routes get data from the db. You don't need to create a new file to retrieve data from database, just edit the api.php file and add the routes you need like

    Route::get('/users',function(){
    User::get();
    });
    

    I solved my problem like this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?