来自登录表单的Ajax json和参数

时间:2012-08-06 12:24:19

标签: jquery ajax json html5 jquery-mobile

我的问题如下:

我正在为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);
                        }
                     })
                };

然后我失败了...... 任何人都知道为什么???

答:* 解答: *解答:* 解答: *

我已经把:authenticate(userName,password); 进入点击功能,如Thrustmaster所说。 完成此操作,因为否则点击时用户名和密码的数据超出范围,如安德鲁所说。

我也把假回报;在.click函数,以便我可以多次单击该按钮(并仍然获得功能验证的结果)。

日Thnx

3 个答案:

答案 0 :(得分:2)

您没有从authenticate(..)活动中致电.click(..)

$("#btnSubmit").click(function () {
    var userName = $("#username").val();
    var password = $("#password").val();
    authenticate(userName,password);
);

authenticate(..)内部应该是:

data: {username: userName, password: password1},

答案 1 :(得分:1)

点击事件后,用户名和密码是否超出范围。或者我的封口错了。

答案 2 :(得分:0)

您没有使用用户名和密码的值:

data: '{username: userName, password: password1 }',

将其更改为:

data: {'username': userName, 'password': password1 },