My question is as follows:
I am creating a login for a jquery mobile application. I am doing this by a standard html login form. After i click the submit button, i am checking if the username and password provided are correct. I check this by using Ajax and json. Now the problem is as follows, if i do the following:
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);
}
})
};
Then i get a success,
But if i want to get the parameters from my fields and do this:
$(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);
}
})
};
Then i get a fail…
Any one know why???
ANSWER:*ANSWER:*ANSWER:*ANSWER:*
I have put : authenticate(userName,password);
into the click function as Thrustmaster said.
Done this because otherwise the data of username and password while clicking are going out of scope as Andrew said.
I’ve also put return false; at the .click function so that i can click the button more than once(and still getting results of function authenticate).
THNX
Aren’t username and password are out of scope after the click event. Or I got my closures wrong.