Json 通过POST HTTPRequest发送参数(appcelerator钛)
我在我的服务器(index.php)上创建了一个页面,当应用程序(通过appcelerator)通过createHTTPClient(app.js和login.js)访问该页面时,返回一个JSON字符串。但是,当我通过POST-HTTPRequest登录并发送参数时,我遇到了问题 index.php:Json 通过POST HTTPRequest发送参数(appcelerator钛),json,post,xmlhttprequest,titanium-mobile,appcelerator-mobile,Json,Post,Xmlhttprequest,Titanium Mobile,Appcelerator Mobile,我在我的服务器(index.php)上创建了一个页面,当应用程序(通过appcelerator)通过createHTTPClient(app.js和login.js)访问该页面时,返回一个JSON字符串。但是,当我通过POST-HTTPRequest登录并发送参数时,我遇到了问题 index.php: <?php class Connect{ public function login($username, $password){ $db = mysqli_connect("mys
<?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']);
?>
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();
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);
});
login.js:
<?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']);
?>
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();
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);
});
结果是:
使用用户名和密码登录时的结果:
任何想法请。对于跨域修复,最简单的答案是在网站上安装LAMP或使用域服务器: (Linux),(Mac OS)和(Windows) 安装此软件包时,请在其中使用html:) 1º由于内容类型必须为application/x-www-form-urlencoded,您的发送有问题 2º函数php login()返回JSON或字符串。。。必须始终为JSON,因为在xhr.onload()处使用JSON.parse
echo“登录不正确”代码>更改为
$response = array(
"logged" => false,
"message" => 'login ou mot de passe est incorrecte'
);
echo json_encode($response);
看起来您正在使用Opera使用Tianium调试您的移动web应用程序。这应该是您的web浏览器“保护”您免受跨域请求的问题。这由XMLHttpRequest错误指示。在Chrome中,有一种方法可以覆盖浏览器的这种安全功能。这应该是一个歌剧的问题,而不是钛。您正在使用的web浏览器正在拒绝该操作。