【发布时间】:2016-04-20 10:20:05
【问题描述】:
我有以下代码。所以基本上JavaScript需要查看用户名和密码并通过API(有效)检查并返回true或false。真让用户访问下一页,假重新加载登录页面。
除了 JavaScript if 语句外,一切都运行良好。
我的代码如下:
var ApiKey = ''; //generated API Key
var userPass = document.getElementById('pass'); //gets the password
var userName = document.getElementById('usr'); //gets the username
function testAJAX(){
$.getJSON("http://mywebsitesapiaddress.com/api" + ApiKey +"&user="+ userName.value + "&pass=" + userPass.value, function(data) {
if (data.success == "true") {
window.location = "mainpage.html";
}
else {
location.reload();
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<input type="text" name="username" id="usr" placeholder="Enter your username">
<input type="password" name="password" id="pass" placeholder="Enter your password">
<button onclick="testAJAX()" id ="login">Login</button>
【问题讨论】:
-
试试
if (data.success == true) {或只是if (data.success) { -
您的回应是什么?你试过
if (data.success){}else{}或if (data.success==true){}else{} -
不相关,但是是什么阻止了某人检查您的来源并手动转到
mainpage.html- 您是否也在该页面上进行了服务器端检查? -
向我们展示返回的 JSON,特别是包含
"success":"true"的 JSON。 -
if(data.success == true ){
标签: javascript jquery json api rest