dqc19941228 2017-06-19 06:05
浏览 64
已采纳

Ionic 2在使用php和mysql发出请求时给出了错误的响应

I'm doing login from an app using php and mysql. When I put the URL on the browser with credentials appended with it, if the credentials are valid, it prints correct response like success:true. If credentials are not valid, it prints success:false. But when I put do ionic serve and do a login from the app, it prints success:true on console all the time, even if the credentials are not valid.

This is my IONIC-2 code:

var headers = new Headers();
headers.append("Accept",'application/json');
headers.append('Content-Type','application/json');
let options = new RequestOptions({headers:headers});
let postParams={
  username: logincreds.username,
  password: logincreds.password,
}
this.http.post("http://vaishalishaadi.com/AppLogin.php", postParams, options)
  .subscribe(data=>{
    console.log(postParams);

    console.log(data);     
    /*if(data.json().success=="success"){

    } 
    else{

    }  */
  }, error => {
    console.log(error);
  });

Following code is of PHP:

  header('Content-type: application/json');
if($check){  
        session_start(); 
    $_SESSION['u_id'] = $check->u_id;
    $_SESSION['u_name'] = $check->u_firstname;
             $login_response=["success"=>"true"];
              //print(json_encode($login_response));
            //echo $check->u_id;
                $data[] = array(
                    'pram'=$username
            'id' => $check->u_id,
            'fname' => $check->u_firstname,
            'lname' => $check->u_lastname,
        );
        $result = "{'success':true, 'data':" . json_encode($data) . "}"; 

    }
    else{
      $result = "{'success':false}";
   }
   echo($result);
  • 写回答

1条回答 默认 最新

  • duanqiangwu9332 2017-06-22 04:32
    关注

    Found Solution Over It Just changed the way I was passing parameters and also removed 'options' argument from post request.

    let params = new URLSearchParams();
    params.append('username',logincreds.username);
    params.append('password',logincreds.password);
    this.http.post(url, params)
      .subscribe(data=>{
    console.log(postParams);
    
    console.log(data);     
    /*if(data.json().success=="success"){
    
    } 
    else{
    
    }  */
    
    }, error => {
    console.log(error);
    })
    

    Use URLSearchParams instead of using array to collect parameters being passed to server

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

报告相同问题?

悬赏问题

  • ¥15 fastreport怎么判断当前页数
  • ¥15 Kylin-Desktop-V10-GFB-Release-JICAI_02- 2207-Build14-ARM64.iso有没有这个版本的系统啊
  • ¥15 能不能通过蓝牙将传感器数据传送到手机上
  • ¥20 100元python和数据科学实验项目
  • ¥15 根据时间在调用出列表
  • ¥15 R 包chipseeker 安装失败
  • ¥15 Veeam Backup & Replication 9.5 还原问题
  • ¥15 vue-print-nb
  • ¥15 winfrom的datagridview下拉框变成了黑色,渲染不成功
  • ¥20 利用ntfy实现短信推送