如何使用Jquery和Json进行身份验证登录

时间:2011-03-31 18:10:01

标签: jquery json passwords username

我有一台服务器,如果我打电话给它将返回json文件。 基本上我有多个可以被调用的用户。

ex: http://server.insource.local/users/145
ex: http://server.insource.local/users/146
ex: http://server.insource.local/users/147

我想以某种方式向服务器发送用户名和密码,如果正确的话,请发回与该用户名和密码匹配的其中一个链接。

这个想法不是使用任何PHP。

我会抓住任何东西,任何想法,任何一个例子。

服务端脚本示例? 感谢

编辑: 我发现的是,链接路径是返回我的信息,并且服务器无论如何要求进行身份验证,所以生病了,只需互相检查一下

谢谢你们

3 个答案:

答案 0 :(得分:3)

您可以考虑使用 jQuery 进行 AJAX 调用。

以下是示例代码:

$.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
        }
    });

当然,您首先必须从 userName 密码字段中收集用户输入的值。

在服务器端,您可以执行所有验证并发回简单消息,例如已验证失败 ajax 调用的成功部分是您在用户通过身份验证后执行的任何过程。

有关 jQuery AJAX http://api.jquery.com/jQuery.ajax/

的更多信息

** 更新 **

假设您有以下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" />

您可以使用此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
            }
        })
    }

答案 1 :(得分:0)

检查以下代码:

$.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
    }
});

答案 2 :(得分:0)

$.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
    }
});