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 微信夜间被转走了1w对,当天手机剪切板里就出现了这个乱码,有铁子可以看看是啥吗可以
  • ¥50 跑通github上的代码 深度学习 pytorch
  • ¥50 求写,批处理调用分区助手分区脚本
  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 【有偿】调用DXGI截图初始化失败,提示0xf 887a0004