dqwh26750 2018-04-17 15:07
浏览 68
已采纳

this.http.post不会发送角度为4的参数

I need to create a login system, I try to send the params to the PHP server.

Angular app

return this.http.post<any>('/s/login/index.php', {
                username: username,
                password: password
            }).map(user => {
                console.log(username); 
                // login successful if there's a jwt token in the response
                if (user) {
                    // store user details and jwt token in local storage to keep user logged in between page refreshes
                }

                return user;
            });
    }

PHP server

if($_SERVER["REQUEST_METHOD"] == "POST"){

 // Check if username is empty
if(empty(trim($_POST["username"]))){
    $username_err = 'Please enter username.';
    $resp->body->message=$username_err;
    $resp=json_encode($resp);
    echo $resp;
}

But the server always returns

{status: "400", body: {message: "Please enter username."}}
  • 写回答

1条回答 默认 最新

  • dongyikong6207 2018-04-17 15:16
    关注

    It is because you are not setting headers.

    first import necessary modules

    import {HttpClient,HttpHeaders} from '@angular/common/http';
    import {URLSearchParams} from '@angular/http';
    

    try it this way.

    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    let options = {
        headers: headers
    };
    
    let body = new URLSearchParams();
    body.set('username', username);
    body.set('password', password);
    
    return this.http.post<any>('/s/login/index.php', body, options).map(user => {
                console.log(username); 
                // login successful if there's a jwt token in the response
                if (user) {
                    // store user details and jwt token in local storage to keep user logged in between page refreshes
                }
    
                return user;
            });
    }
    

    Tell me if it does not work. I am here !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备