我尝试在 Phonegap 中登录并尝试将其连接到我的数据库
当我使用“邮递员”时它起作用
它会提醒正确的电子邮件+密码组合 它警告空字符串 它记录和空对象
我做错了什么?由于我从服务器收到响应但为空,似乎我的参数没有正确传输
$("#submit").click(function() {
var email = $("#username").val();
var password = $("#password").val();
if (email == '' || password == '') {
} else {
$.ajax({
type: 'GET',
url: 'http://xxxxx.php',
crossDomain: true,
data: {
email: 'email',
password: 'password'
},
dataType: 'json',
success: function(JSONObject) {
alert(email + password);
alert(JSON.stringify(JSONObject));
console.log(JSONObject);
},
error: function() {
//console.error("error");
alert('Not working!');
}
});
}
});
登录.php
<?php
//Importing Database Script
require_once('dbConnect.php');
//GETS
$email=$_GET['email'];
$pass=$_GET['password'];
//Creating sql query
$sql = "SELECT * FROM `customerdata` WHERE customerNo = '$email' AND customerName = '$pass'";
//getting results
mysqli_set_charset($con,'utf8');
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing information in the blank array created ID, deviceID, terminalnumber, qrcode, cloakroomsection, cloakroomnumber, delivered, collected
array_push($result,array(
"customerName"=>$row['customerName'],
"customerNo"=>$row['customerNo']
));
}
//$finalresult= array();
//$finalresult['1'] = $result;
//Displaying the array in json format
echo json_encode($result);
//echo json_encode($encoded_rows);
mysqli_close($con);
?>
除了 Hamza Dairywala 响应
Access-Control-Allow-Origin
和 content-rype
标头之外,请确保在 config.xml 中添加白名单插件并设置对 * 的访问权限,如下所示:
<access origin="*"/>
<plugin name="cordova-plugin-whitelist" spec="~1.3.0"/>
只需尝试以下代码。
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
//Importing Database Script
require_once('dbConnect.php');
//GETS
$email=$_GET['email'];
$pass=$_GET['password'];
//Creating sql query
$sql = "SELECT * FROM `customerdata` WHERE customerNo = '$email' AND customerName = '$pass'";
//getting results
mysqli_set_charset($con,'utf8');
$r = mysqli_query($con,$sql);
//creating a blank array
$result = array();
//looping through all the records fetched
while($row = mysqli_fetch_array($r)){
//Pushing information in the blank array created ID, deviceID, terminalnumber, qrcode, cloakroomsection, cloakroomnumber, delivered, collected
array_push($result,array(
"customerName"=>$row['customerName'],
"customerNo"=>$row['customerNo']
));
}
//$finalresult= array();
//$finalresult['1'] = $result;
mysqli_close($con);
header('content-type: application/json; charset=utf-8');
//Displaying the array in json format
echo json_encode($result);
//echo json_encode($encoded_rows);
?>