0
我在cordova項目中有一個頁面,它嘗試在用戶按下登錄按鈕以使用我的REST腳本對用戶進行身份驗證時發送AJAX請求。奇怪的是,如果我只是把AJAX代碼放在就緒函數中,但是如果我把它放在一個事件處理程序中,它就會得到一個404的狀態代碼。下面是我現在使用的代碼。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<!-- Set the viewport settings to prevent scaling -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, target-densitydpi=device-dpi" />
<!-- Makes your prototype chrome-less once bookmarked to your phone's home screen -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>Login</title>
<link rel="stylesheet" type="text/css" href="css/index.css" />
<link rel="stylesheet" href="css/ratchet.css">
<meta name="msapplication-tap-highlight" content="no" />
<script src="js/jquery-2.1.1.min.js"></script>
<script>
$(document).bind("mobileinit", function(){
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
});
</script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script src="js/ratchet.js"></script>
<script type="text/javascript">
app.initialize();
function auth(username, password){
$.getJSON("http://devserver/REST.php?callback=?", {method: "mobileAuthUser", username: username, password: password}, function(response, status, xhr){
alert(response);
}).error(function(xhr, ajaxOptions, thrownError){
alert("error");
alert(xhr.status);
alert(thrownError);
});
}
$(function(){
auth("username", "password");
$("#subButton").click(function(){
auth("username", "password");
});
});
</script>
<style>
/*Stops the text from being in all caps*/
body * {
text-transform: none;
}
</style>
</head>
<body>
<header class="bar bar-nav">
<h1 class="title">Login</h1>
</header>
<div class="content">
<form>
<input type="text" id="username" placeholder="username">
<input type="password" id="password" placeholder="password">
<button class="btn btn-positive btn-block" id="subButton">Login</button>
</form>
</div>
<div id="deviceready">
<p class="event listening">Connecting to Device</p>
<p class="event received">Device is Ready</p>
</div>
</body>
</html>
焦點件這裏是
auth("username", "password");
$("#subButton").click(function(){
auth("username", "password");
}
的auth()
頁面時準備的作品就好了,這只是運行。在點擊按鈕時運行的auth()
將返回一個錯誤,並在錯誤回調中給出錯誤代碼404.我不明白的是,我如何才能讓完全相同的函數調用失敗,僅僅因爲它在一個事件處理器。