如何使用 Jquery 和 Json 进行登录身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5504352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do a log in authentication using Jquery and Json
提问by Patrioticcow
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.
我有一个服务器,如果我调用它会返回 json 文件。基本上我有多个可以调用的用户。
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.
这个想法是不使用任何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
谢谢你们
回答by Quad Coders
You could consider AJAXcall with jQuery.
您可以考虑使用jQuery 进行AJAX调用。
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 userNameand passwordfields.
当然,您首先必须从用户名和密码字段中收集用户输入的值。
On the server side, you can do all the verification and send back simple message such as authenticatedor failed. The successpart of the ajaxcall is for you to do any process after the user is authenticated.
在服务器端,您可以进行所有验证并发回简单的消息,例如authenticated或failed。ajax调用的成功部分是让您在用户通过身份验证后进行任何处理。
More information about jQuery AJAXhttp://api.jquery.com/jQuery.ajax/
有关jQuery AJAX 的更多信息http://api.jquery.com/jQuery.ajax/
** UPDATE**
**更新**
Say you have this HTML codes:
假设您有以下 HTML 代码:
<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:
您可以使用此 jQuery 脚本收集用户的输入并进行 ajax 调用:
$(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
}
})
}
回答by MRitunjay
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
}
});
回答by demo
$.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
}
});