我的问题如下:Ajax json和参数从登录表格
我正在创建一个jQuery的移动应用程序的登录。我正在通过标准的HTML登录表单来完成此操作。当我点击提交按钮后,我正在检查提供的用户名和密码是否正确。我通过使用Ajax和json来检查这一点。现在的问题是如下,如果我做到以下几点:
function authenticate(userName, password1) {
$.ajax
({
type: "POST",
//the url where you want to sent the userName and password to
url: "http://notgiven",
dataType: 'json',
async: false,
//json object to sent to the authentication url
//data: {"username": "' + userName + '", "password" : "' + password + '"},
data: { username: "user1", password: "test1" },,
success: function (data) {
//do any process for successful authentication here
alert('Login status: ' + data.status);
}
})
};
然后我得到了成功,
但是,如果我想从我的田地参数和做到这一点:
$(document).ready(function() {
//event handler for submit button
$("#btnSubmit").click(function() {
//collect userName and password entered by users
var userName = $("#username").val();
var password = $("#password").val();
});
});
function authenticate(userName, password1) {
$.ajax
({
type: "POST",
//the url where you want to sent the userName and password to
url: "http://notgiven",
dataType: 'json',
async: false,
//json object to sent to the authentication url
//data: '{"username": "' + userName + '", "password" : "' + password + '"}',
data: '{username: userName, password: password1 }',
success: function (data) {
//do any process for successful authentication here
alert('Login status: ' + data.status);
}
})
};
然后我得到一个失败... 任何人知道为什么?
答:*答: *答:*答: *
我已经把:认证(用户名,密码);如Thrustmaster所说的成为点击功能的 。 完成此操作,否则在单击时用户名和密码的数据超出范围,如Andrew所说。
我也把返回false;在.click函数中,这样我可以多次点击按钮(并且仍然获得功能验证的结果)。
日Thnx
你是对的! – bdz 2012-08-06 12:38:24