2017-10-07 29 views
0

這是php代碼,這裏我要求離子3的用戶名和密碼做一個select查詢如果我的sql中有數據,我會返回到離子3接受,如果沒有,拒絕這是登錄頁面。我在離子3中遇到了一個錯誤:運行時錯誤意外的標記<在位置0的JSON中發佈到來自ionic3的php時

$Username= filter_var($_REQUEST['Username'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW); 
$Password = filter_var($_REQUEST['Password'], FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW); 
$sql="SELECT Username, Password FROM doctoradd WHERE Username = :Username AND Password = :Password"; 
     //$sql = "INSERT INTO technologies(name, description) VALUES(:Username, :Password)"; 
     $stmt = $pdo->prepare($sql); 
     $stmt->bindParam(':Username', $Username, PDO::PARAM_STR); 
     $stmt->bindParam(':Password', $Password, PDO::PARAM_STR); 
     $stmt->execute(); 
     while($row = $stmt->fetch(PDO::FETCH_OBJ)) 
     { 
     // Assign each row of data to associative array 
     $data[] = $row; 
     } 
     if($data == []){ 
      echo json_encode("null"); 
     } 
     else{ 
      echo json_encode("accepted"); 

     } 

我離子3碼:這是按鈕點擊功能

let Username : string = this.form.controls["Username"].value, 
Password : string = this.form.controls["Password"].value; 

    let body  : string = "Username=" + Username + "&Password=" + Password, 
    type  : string = "application/x-www-form-urlencoded; charset=UTF-8", 
    headers : any  = new Headers({ 'Content-Type': type}), 
    options : any  = new RequestOptions({ headers: headers }), 
    url  : any  = this.baseURI + "sign-in.php"; 

this.http.post(url, body, options) 
.subscribe((data) => 
{ 
    // If the request was successful notify the user 
    // if(data.status === 200) 
    // { 
    // //this.hideForm = true; 

    // this.toast.create({ 
    //  message: 'Welcome ', 
    //  duration:3000 


    //   }).present(); 
    // //this.sendNotification(`Congratulations the technology: ${username} was successfully added`); 



    // } 
    // // Otherwise let 'em know anyway 
    // else 
    // { 
    // //this.sendNotification('Something went wrong!'); 
    // this.toast.create({ 
    //  message: 'Wrong Username or Password', 
    //  duration:3000 


    //   }).present(); 

    // } 



}); 


this.SignIn(); 

}

的登入功能,我想取回json_encode在PHP如果被接受與否

SignIn(){ 
this.http.get('http://localhost:10080/testdb/sign-in.php') 
.map(res => res.json()) 
.subscribe(data => 
{ 
    this.items = data; 
}); 
alert(this.items); 
if(this.items=="accepted"){ 
    this.navCtrl.push('ExamplePage',this.username); 
} 
else{ 
    this.toast.create({ 
    message: 'Wrong Username or Password', 
    duration:3000 
      }).present(); 
} 

}

回答

0

這個錯誤發生在映射函數中。您收到的回覆不是JSON格式,因此您無法將其映射到res.json()

請檢查您在Postman或某個工具中的回覆。如果不能消除地圖功能並檢查響應;

SignIn(){ 
this.http.get('http://localhost:10080/testdb/sign-in.php') 
.map(res => res) 
.subscribe(data => 
{ 
    console.log(res); 
    // this.items = data; 
}); 
alert(this.items); 
if(this.items=="accepted"){ 
    this.navCtrl.push('ExamplePage',this.username); 
} 
else{ 
    this.toast.create({ 
    message: 'Wrong Username or Password', 
    duration:3000 
      }).present(); 
} 
+0

我只是刪除了.MAP並停止給我,但「this.items」點擊按鈕,但兩個輸入值是在數據庫中,你可以幫我這個當是給一個空值的錯誤! !謝謝你 –

+0

你可以控制檯登錄你的回覆併發送給我嗎? –

+0

console.log(res)發生錯誤:無法找到名稱'res'。我可以在沒有.map的情況下使用它嗎?我用它沒有它,但this.items按鈕點擊賦予空值,你可以幫助這個? –

相關問題