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

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
    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

    点赞 评论

相关推荐