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 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?