我在我的服务器上创建了一个页面(index.php),当应用程序(通过appcelerator titanium)通过createHTTPClient(app.js和login.js)访问时,返回一个JSON字符串。但是当我通过POST HTTPRequest登录并发送参数时,我遇到了问题。
index.php:
<?php
class Connect{
public function login($username, $password){
$db = mysqli_connect("mysql6.000webhost.com", "a8324766_user", "**********", "a8324766_db");
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT *
FROM users
WHERE username ='$username'
AND password = '$password'";
$req = mysqli_query($db, $sql);
$data = mysqli_fetch_assoc($req);
if(isset($data['username'])&& !empty($data['username']) && isset($data['password'])&& !empty($data['password'])){
if (mysqli_num_rows($req) > 0){
$response = array(
"logged" => true,
"name" => $data['name'],
"email" => $data['email']);
echo json_encode($response);
}
else
{
// Else the username and/or password was invalid! Create an array, json_encode it and echo it out
$response = array(
"logged" => false,
"message" => 'Invalid Username and/or Password'
);
echo json_encode($response);
}
}else{
echo "login ou mot de passe est incorrecte";
}
}
}
$user = new Connect();
$user->login($_POST['username'], $_POST['password']);
?>
app.js:
Titanium.UI.setBackgroundColor('#fff');
var tabGroup = Titanium.UI.createTabGroup();
var login = Titanium.UI.createWindow({
title:'User Authentication Demo',
tabBarHidden:true,
url:'login.js'
});
var loginTab = Titanium.UI.createTab({
title:"Login",
window:login
});
tabGroup.addTab(loginTab);
tabGroup.open();
login.js:
var win = Titanium.UI.currentWindow;
var username = Titanium.UI.createTextField({
color:'#336699',
top:10,
left:10,
width:300,
height:40,
hintText:'Username',
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(username);
var password = Titanium.UI.createTextField({
color:'#336699',
top:60,
left:10,
width:300,
height:40,
hintText:'Password',
passwordMask:true,
keyboardType:Titanium.UI.KEYBOARD_DEFAULT,
returnKeyType:Titanium.UI.RETURNKEY_DEFAULT,
borderStyle:Titanium.UI.INPUT_BORDERSTYLE_ROUNDED
});
win.add(password);
var loginBtn = Titanium.UI.createButton({
title:'Login',
top:110,
width:90,
height:35,
borderRadius:1,
font:{fontFamily:'Arial',fontWeight:'bold',fontSize:14}
});
win.add(loginBtn);
var xhr = Ti.Network.createHTTPClient({timeout:50000});
xhr.onload = function(e) {
Ti.API.info("Received text: " + this.responseText);
var json = this.responseText;
var response = JSON.parse(json);
if (response.logged == true){
alert("Welcome " + response.name + ". Your email is: " + response.email);
} else {
alert(response.message);
}
};
xhr.onerror = function(e) {
Ti.API.info('Error >>>> ' + JSON.stringify(e));
};
loginBtn.addEventListener('click',function(e){
xhr.open("POST","http://lahcene.comli.com/index.php");
var params = {
username: username.value,
password: password.value
};
alert(username.value);
alert(password.value);
xhr.setRequestHeader( 'Content-Type','application/json' );
xhr.send(params);
});
结果:
http://i.stack.imgur.com/OfByi.png
使用用户名和密码登录的结果:
http://i.stack.imgur.com/9qm1m.png
请任何想法。
答案 0 :(得分:0)
修复跨域的最简单答案是LAMP或在网站上使用域名服务器:
Install Lamp(Linux),Install Mamp(Mac OS)&amp; Install Xampp(Windows)
安装此软件包时,请使用其中的html:)
1º您的发送存在问题,因为Content-Type必须 application / x-www-form-urlencoded
2º你的函数php login()返回一个JSON或String ...必须是JSON总是因为你在xhr.onload()使用JSON.parse。
echo "login ou mot de passe est incorrecte";
更改为
$response = array(
"logged" => false,
"message" => 'login ou mot de passe est incorrecte'
);
echo json_encode($response);