Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework Ionic2-http get不工作_Ionic Framework_Ionic2_Http Get - Fatal编程技术网

Ionic framework Ionic2-http get不工作

Ionic framework Ionic2-http get不工作,ionic-framework,ionic2,http-get,Ionic Framework,Ionic2,Http Get,我已经编写了一个身份验证服务来验证登录页面中的用户名和密码。下面的代码是服务代码 public login(credentials) { if (credentials.username === null || credentials.password === null) { return Observable.throw("Please insert credentials"); } else { let apiURL = 'http://localhost/t

我已经编写了一个身份验证服务来验证登录页面中的用户名和密码。下面的代码是服务代码

public login(credentials) {
   if (credentials.username === null || credentials.password === null) {
      return Observable.throw("Please insert credentials");
   } else {

   let apiURL = 'http://localhost/timeclock/api/login?usercode=' + credentials.username +
   '&password=' + credentials.password ;

   return Observable.create(observer => {     
    this.http.get(apiURL).map(res => res.json()).subscribe(data => {        
    if (data.success === 'true')
    {
      this.currentUser.name  = data.data.user_name;
      this.currentUser.email = data.data.user_email;
      observer.next(true);
      observer.complete();
    } else {
        observer.next(false);
        observer.complete();
      }      
    });
  });
}
}
提交用户名和密码后,将使用正确的参数正确调用URL

http调用需要很长时间才能完成。此外,不会返回任何响应

当我在浏览器中使用相同的参数调用URL时,只需两三秒钟就可以得到响应


你知道如何解决这个问题吗?

你不需要创建一个新的可观察对象,你可以像这样重构

public login(credentials) : Observable<boolean> {
  if (credentials.username === null || credentials.password === null) {
    return Observable.throw("Please insert credentials");
  } else {

    let apiURL = 'http://localhost/timeclock/api/login?usercode=' + credentials.username +
      '&password=' + credentials.password ;

    return this.http.get(apiURL).map(res => res.json())
      .map(data => 
      {
        if(data.success){
          this.currentUser.name  = data.data.user_name;
          this.currentUser.email = data.data.user_email;
          return true;
        }else{
          return false;
        }
      });
  }
}
公共登录(凭证):可观察{
if(credentials.username==null | | credentials.password==null){
return Observable.throw(“请插入凭证”);
}否则{
让我们来看看http://localhost/timeclock/api/login?usercode=“+credentials.username+
“&password=”+credentials.password;
返回此.http.get(apirl.map)(res=>res.json())
.map(数据=>
{
if(data.success){
this.currentUser.name=data.data.user\u name;
this.currentUser.email=data.data.user\u email;
返回true;
}否则{
返回false;
}
});
}
}

您是否尝试过设置标题。此外,您没有设置任何错误的捕获。我建议您使用
POST
进行登录,这样在URL中就看不到密码,您仍然可以检索返回的值。现在,你的问题来了。如果您的
GET
请求确实已发送,您是否可以在chrome开发者工具中进行检查,以及响应是什么?谢谢您的评论。我发现第一次调用api时,调用需要很长时间。它在随后的调用中工作良好。关于空响应,我发现在发送响应之前需要在API响应中设置头。现在,我从API收到了这些值。我对爱奥尼亚是个新手。我们什么时候使用Observable.create?我设法使代码按照上面的注释工作。当您想要创建自己的自定义可观察对象时。可观测数据来自RxJS库,用于检查官方文档。阅读本网站了解它。我建议你们学习它,因为离子2和角2非常关注可观测物。