Angular 2待办事项应用程序不在MySQL数据库中存储新任务

时间:2017-01-18 05:12:10

标签: mysql angular ionic2

我正在做this tutorial using Ionic 2 and Angular 2 to create a to-do app。我发誓我正在输入他在视频中给我们的确切代码,但我的应用程序并没有将项目存储到数据库中。 (MySQL数据库运行正常。)尝试时,我收到此错误:

Response_body: "failed"
headers: Headers
ok: true
status: 200
statusText: "OK"
type: 2
url: "http://localhost/note/store.php"
__proto__: Body
it fails!   home.ts:25

和" [object Object]"在console.log中。所以请求已经到了某个地方,但由于某些原因它没有得到保存,我无法说明原因。

老师(导师?Tutorialer?)查看了我的详细信息,并说他看不到将函数发送到store.php。我的理解是home.ts中的这个功能可以做到:

  addDB(obj){
    this.http.post("http://192.168.1.162/note/store.php",obj).
    subscribe(data =>{
      console.log(data);
      var resp = data.text().trim();
      if(resp == "success"){
        console.log("it works!");
        this.loadDB();
      }else{
        console.log("it fails!");
      }
    }, err=>{
      console.log(err);
    })
  }

但是当我这样说时,他似乎并不清楚自己需要什么。我想也许StackOverflow会知道该怎么做。

有多个.ts和.html以及.php文件,所以我无法做出小提琴; I've put them all up on GitHub instead。具体来说,home.html and home.tsapp.module.tsdetail.html and detail.tsthese php files是我唯一触及过的。

我觉得这必须是简单而荒谬的事情,例如"这个功能在错误的文件中,你没有注意到他在这里切换了他正在使用的那个"但我发誓我已经反复看过来了......

1 个答案:

答案 0 :(得分:3)

在角度2中使用http时,您必须使用.map()函数解析响应,如下所示:

this.http.post("http://192.168.1.162/note/store.php",obj)
    .map(res => res.json())
    .subscribe(data=>{

    },
    err=>{

    })

编辑:使用Promise而不是observable:

import 'rxjs/add/operator/toPromise';

this.http.post("http://192.168.1.162/note/store.php",obj)
    .toPromise()
    .then(data=>{

    })
    .catch(err=>{

    })