2016-11-21 65 views
0

我需要在我的Ionic 2应用程序中实现登录。现在,我只需确保电子邮件和密码得到正确接收并获得基本回复。Ionic 2 - 如何提出登录请求?

我已经成功地测试了一个GET请求到API,它正常工作,但我无法获得POST请求的工作。

我已经尝试了两种方法:

  1. 使用subscribe

    登录组件:

    submitLogin() 
    { 
        var email = this.loginForm.value.email.trim(); 
        var password = this.loginForm.value.password.trim(); 
    
        this.userService.makeLogin(email, password);  
    } 
    

    服务:

    makeLogin(email: string, password: string) 
    { 
        var body = JSON.stringify({ 
         email: email, 
         password: password 
        }); 
    
        var headers = new Headers(); 
        headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
    
        this.http 
         .post(this.loginUrl, body, { headers: headers }) 
         .subscribe(data => { 
            console.log('login API success'); 
            console.log(data); 
           }, error => { 
            console.log(JSON.stringify(error.json())); 
         }); 
    } 
    

    的API(笨):(代码最低为测试目的)

    public function testLoginIonic2() 
    { 
        header('Access-Control-Allow-Origin: *'); 
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 
    
        $account_email = $this->input->post('email'); 
        $account_password = $this->input->post('password'); 
    
        echo json_encode($account_email); 
    } 
    

    结果:

    enter image description here

    到服务器的连接,似乎好 - 但没有数据被返回 - 响应的主体为空

  2. 使用Promise

    登录组件:

    submitLogin() 
    { 
        var email = this.loginForm.value.email.trim(); 
        var password = this.loginForm.value.password.trim(); 
    
        this.userService.testLogin(email, password) 
          .then(data => { 
           console.log('response data'); 
           console.log(data); 
          }); 
    } 
    

    服务:

    testLogin(email: string, password: string): Promise<any> 
    { 
        var body = JSON.stringify({ 
         email: email, 
         password: password 
        }); 
    
        var headers = new Headers(); 
        headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 
    
        return this.http 
         .post(this.loginUrl, body, {headers: headers}) 
         .toPromise() 
         .then(res => res.json().data); 
    } 
    

    API:

    前结果:

    enter image description here

    我收到此错误信息:Unexpected end of JSON input(可能是因为响应是空的,因此不能以JSON格式)。

为什么我会得到一个空的回应?

表单正常工作。错误不在那里。

所以请求中肯定有错,但是什么?

编辑:

测试与波兹曼看来,参数是空的。可能它们没有正确发送。

+0

因为email是字符串类型,我认为它应该是'echo json_encode({“email”:$ account_email});'因为你期望数据是json格式。我不知道codeIgniter,所以只是一个建议:-) –

+0

以邮递员为例,并尝试调用您的后端。如果它没有返回你期望的结果,那么pb来自你的后端,并且与前端或者angular2无关。 – Maxime

+0

非常好的建议!这似乎确实没有正确发送参数。这就是为什么要清空响应。在Postman中,如果将参数设置为原始数据,则会发生相同的情况。如果我将它们作为x-www-form-urlencoded或表单数据输入,那么我会正确回复电子邮件作为回复。 – johnnyfittizio

回答

3

感谢您的回复。

@ hgoebl的回答正常。

我发现了一个解决方案,是一个比较简单的使用URLSearchParams

组件

submitLogin() 
{ 
    var email = this.loginForm.value.email.trim(); 
    var password = this.loginForm.value.password.trim(); 

    this.userService.makeLogin(email, password) 
     .subscribe((response) => { 
      console.log(response); 
     }, (error) => { 
      console.log(error); 
     }); 
} 

服务

makeLogin(email: string, password: string) 
{  
    let urlSearchParams = new URLSearchParams(); 
    urlSearchParams.append('email', email); 
    urlSearchParams.append('password', password); 
    let body = urlSearchParams.toString() 

    var headers = new Headers(); 
    headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 

    return this.http 
     .post(this.loginUrl, body, { headers: headers }) 
     .map(response => response.json()) 
} 

记住URLSearchParams有请在服务中输入:

import { Http, Headers, URLSearchParams } from '@angular/http'; 

EDIT

我移动部件内的订阅能够处理组件内的响应数据。

1

您不应该在http.post之后做map吗?

this.http 
    .post(this.loginUrl, body, { headers: headers }) 
    .map(response => response.json()) 
    .subscribe(data => { 
     console.log('login API success'); 
     console.log(data); 
    }, e => { 
     console.log(e); 
    }); 

您对observables的回复看起来不错,但没有解析。

+0

你是对的!它失踪了!以这种方式改变我得到答复的方式。不是一个字符串,而是一个干净的json。谢谢! – johnnyfittizio

1

对我来说,如果问题是以错误的格式发送的,那么看起来好像是这样。 你告诉服务器它的内容是application/x-www-form-urlencoded,但是你发送的是JSON。

你可以试试这个:

testLogin(email: string, password: string): Promise<any> 
{ 
    var headers = new Headers(); 
    headers.append('Content-Type', 
     'application/x-www-form-urlencoded; charset=UTF-8'); 

    let params = {email, password}; 
    let body = this.toFormUrlEncoded(params); 

    //... your post request 
} 

private toFormUrlEncoded(object: Object): string { 
    return Object.keys(object) 
     .map(key => { 
      return encodeURIComponent(key) + '=' + 
        encodeURIComponent(object[key]); 
     }) 
    .join('&'); 
} 

作为替代你也可以改变你的内容类型,以JSON和更改服务器解码JSON。

+0

谢谢你的回答! – johnnyfittizio