gpt4 book ai didi

javascript - AJAX responseText 字符串比较

转载 作者:行者123 更新时间:2023-11-30 17:59:16 29 4
gpt4 key购买 nike

我无法将准确的字符串与我的 AJAX 脚本的结果进行比较。出于某种原因,我的 if 语句总是失败。通过删除 if 测试,我可以看到返回了正确的值(“成功”),但是当我保留它时,它的计算结果为 false。我究竟做错了什么?谢谢。

HTML 文件:

PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Sign In</title>
<link rel="stylesheet" type="text/css" href="styles/mystyles.css" media="screen" />
</head>

<body>

<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
window.onload = function ajaxFunction() {
document.loginform.onsubmit = attemptLogin;
}

function attemptLogin() {
var username = document.getElementById("name").value;
var password = document.getElementById("password").value;
var xmlhttp;

if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var response = $.trim(xmlhttp.responseText).toLowerCase();
//var response = xmlhttp.responseText.trim();
//var response = xmlhttp.responseText;
if (response == "success") {
// window.location.href = "dashboard.html";
//} else {
document.getElementById("feedback").innerHTML=response;
//}
}
}

xmlhttp.open("GET",
"scripts/login.php?username="+username+"&password="+password
,true);
xmlhttp.send();
return false;
}
//-->
</script>

<div id="frame">
<div id="page">
<img id="mainpic" src="images/banner.png">

<div id="leftsidebox"></div>

<div id="stylized" class="myform">
<form id="form" name="loginform">
<h1>Mobile App Data Administration</h1>
<p>Sign in to update, modify, or delete data used by the mobile app.</p>

<label>Username
<span class="small">Enter your username</span>
</label>
<input type="text" name="username" id="name" autofocus="autofocus" />

<label>Password
<span class="small">Enter your password</span>
</label>
<input type="password" name="pass" id="password" />

<div id="feedback"></div>

<button type="submit">Log In</button>

</form>

<button type="submit" class="lowerbutton" onClick="parent.location='newuser.html'">New User</button>
</div>

</div>
</div>
</body>

</html>

相关的 php 脚本部分只是最后一行:

echo "success";

最佳答案

试一试这个函数,让我们看看会收到什么警报

function attemptLogin()
{
var username = document.getElementById("name").value;
var password = document.getElementById("password").value;
var xmlhttp;

if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
var response = xmlhttp.responseText;
if (response == "success")
{
alert("log you in");
window.location.href = "dashboard.html";
}
else
{
alert("not equal to success");
document.getElementById("feedback").innerHTML = response;
}
}
}

xmlhttp.open("GET", "scripts/login.php?username=" + username + "&password=" + password, true);
xmlhttp.send();
return false;
}

关于javascript - AJAX responseText 字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17435262/

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