How to do a log in authentication using Jquery and Json
i have a server that if i make a call to it will return json files. Basically i have multiple users that开发者_开发知识库 can be called.
ex: http://server.insource.local/users/145
ex: http://server.insource.local/users/146
ex: http://server.insource.local/users/147
What i want to do it to, somehow, send a username and password to the server, if correct send back one of those links that match that username and password.
The idea is not to use any php.
i will grab anything, any idea, any example.
a serve-side script example? thanks
edit: what i find out was that the link path is what is returning my info, and that the server asks for authentication anyway, so ill just check them one against each other
thanks guys
You could consider AJAX call with jQuery.
Here is sample code:
$.ajax
({
type: "POST",
//the url where you want to sent the userName and password to
url: "http://your-url.com/secure/authenticate.php",
dataType: 'json',
async: false,
//json object to sent to the authentication url
data: '{"userName": "' + userName + '", "password" : "' + password + '"}',
success: function (){
//do any process for successful authentication here
}
});
Of course, you would first have to collect user's entered value from userName and password fields.
On the server side, you can do all the verification and send back simple message such as authenticated or failed. The success part of the ajax call is for you to do any process after the user is authenticated.
More information about jQuery AJAX http://api.jquery.com/jQuery.ajax/
** UPDATE **
Say you have this HTML codes:
<input type="text" name="username" id="username" class="text" maxlength="30" />
<br />
<input type="password" name="password" id="password" class="text" maxlength="30" />
<br />
<input type="submit" name="btnSubmit" id="btnSubmit" />
You can use this jQuery script collect user's input and make ajax call:
$(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();
//call the authenticate function
authenticate(userName, password);
});
});
//authenticate function to make ajax call
function authenticate(userName, password) {
$.ajax
({
type: "POST",
//the url where you want to sent the userName and password to
url: "http://your-url.com/secure/authenticate.php",
dataType: 'json',
async: false,
//json object to sent to the authentication url
data: '{"userName": "' + userName + '", "password" : "' + password + '"}',
success: function () {
//do any process for successful authentication here
}
})
}
Check following code:
$.ajax
({
type: "POST",
//the url where you want to sent the userName and password to
url: "http://your-url.com/secure/authenticate.php",
dataType: 'json',
async: false,
//json object to sent to the authentication url
data: '{"userName": "' + userName + '", "password" : "' + password + '"}',
success: function (){
//do any process for successful authentication here
}
});
$.ajax
({
type: "POST",
//the url where you want to sent the userName and password to
url: "http://your-url.com/secure/authenticate.php",
dataType: 'json',
async: false,
//json object to sent to the authentication url
data: '{"userName": "' + userName + '", "password" : "' + password + '"}',
success: function (){
//do any process for successful authentication here
}
});
精彩评论