gpt4 book ai didi

javascript - 登录后重定向到应用程序页面不在同一服务器上

转载 作者:行者123 更新时间:2023-12-03 08:36:26 25 4
gpt4 key购买 nike

我有一个带有登录屏幕的 HTML5 应用程序。当我输入详细信息时,它会发送到外部服务器,运行一个名为 login.php 的 php 文件并检查详细信息。

如果详细信息正确,我需要它重定向回 HTML5 应用程序到 index.html 文件上 id #home 的页面。

如果index.html 和login.php 都位于服务器上,则 header 方法可以正常工作。但现在,html 文件作为 HTML5 应用程序保存在手机上,它会连接到服务器(这是可能的 - 我有服务器 URL)。检查凭据并重定向。它如何重定向回我手机上的应用程序?手机上没有该应用程序的 URL。

也尝试过ajax,但没有任何反应。

PS:如果您打算举报此问题,请先通读并理解该问题。某些文本匹配并不意味着它是同一个问题。

您输入登录详细信息的应用程序的首页:

<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="scripts.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
</head>
<body>
<div data-role="page" id="loginForm">
<form id="form1" name="form1" method="POST" action="http://www.examplewebsite.com/login.php">
<input type="text" name="user" id="user" placeholder="Username"/>

<input type="password" name="pass" id="pass" placeholder="Password" />

<input type="submit" name="submit" value="Login" />
</form>
</div>
<div data-role="page" id="home">
<h1>Logged In</h1>
</div>
</body>
</html>

用于检查登录的脚本。此 php 文件位于服务器端。

//DB Log in credentials
$hostName = 'localhost';
$dbUser = 'fakeuser';
$dbPass = 'fakepass';
$dbName = 'fakedb';
$userTable = "faketable";

//Connect to DB
$conn = mysql_connect($hostName, $dbUser, $dbPass) or die("not connecting");
$dbSelect = mysql_select_db($dbName) or die("no db found");

//Obtain input username and password from the client
$username = $_POST["user"];
$password = $_POST["pass"];

//Check for MySql Injections
if(ctype_alnum($username) && ctype_alnum($password)){
$query1 = mysql_query("SELECT * FROM $userTable WHERE username='$username'");
//query will return 1 if the username exists in the database
$numrows = mysql_num_rows($query1);

if($numrows == 1){
//checking if the password matches the username now
$query2 = "SELECT password FROM $userTable WHERE username='$username'";
$result2 = mysql_query($query2);
$row = mysql_fetch_array($result2, MYSQL_ASSOC);
$pass = $row['password'];

if($password == $pass){
//If successful, redirect to the #home page
//anything I can do here to redirect back to #home on my app?
}
else
echo "Password incorrect";
}
else
echo "username incorrect" . $numrows;
}
else{
echo "Not alpha Numeric Input!!";
}

尝试 Ajax 部分

var isLogged = false;
/**
* Method used to log into the application
*/
$(document).on("pageinit", "#loginForm", function () {
$("#form1").on("submit", function (event) {
event.preventDefault();
$.ajax({
type: "GET",
url: "http://www.examplewebsite.com/login.php",
data: $("#form1").serialize(),
success: function (data) {
console.log(data);
if (data.loggedIn) {
isLogged = true;
$.mobile.changePage("#home");
} else {
alert("You entered the wrong username or password. Please try again.");
}
}
});
});
});

最佳答案

loggedIn 在哪里定义的?你永远不会进入这个范围 if (data.loggedIn) { }, or?

您是否尝试过“返回”json 编码数组并实际使用该数据?

据我所知,您并没有真正使用用户可能遇到的不同错误,即“密码不正确”、“用户名不正确”和“不是字母数字输入!!”。

您可能想做类似的事情:

if (data.loggedIn) { /* Went well */ }
else if (data.passIncorrect) { /* Password incorrect */ }
else if (data.userIncorrect) { /* User incorrect */ }
else if (data.passIncorrect) { /* NaN */ }

您也许可以找到有关该主题的更多信息 herehere .

我不知道这是否有任何帮助,我什至可能在这里偏离了主题。

关于javascript - 登录后重定向到应用程序页面不在同一服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33200665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com