将Javascript变量传递给AJAX调用不起作用

时间:2017-12-17 19:52:52

标签: javascript ajax

当我将变量传递给AJAX时,如果我只是创建一个带有一些字符串值的变量,那么它就可以工作了。 var userid = "test"var passwd = "test1",但不在变量包含var userid = form.userid.valuevar passwd = form.passwrd.value时。为什么form.userid.valueform.passwrd.value不起作用?

这是HTML:

<html>
    <head>
        <title>
        Login page
        </title>
        <script src="login.js"></script>
        <!-- jQuery -->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

        <!-- jQuery -->
        <script src="./jquery.rest.min.js"></script>
        <script src="./webtoolkit.base64.js"></script>
    </head>
    <body>
        <h1 style="font-family:Comic Sans Ms;text-align="center";font-size:20pt;
        color:#00FF00;>
        Simple Login Page
        </h1>
        <form name="login">
            Username<input type="text" name="userid"/>
            Password<input type="password" name="pswrd"/>
            <input type="button" onclick="checkLogin(this.form)" value="Login"/>
            <input type="reset" value="Cancel"/>
        </form>
    </body>
</html>

这是javascript不起作用(login.js):

function checkLogin(form) {
    var auth_token = localStorage.getItem("token");

    //the variables which do not correctly pass through to AJAX call
    var userid = form.userid.value;
    var passwd = form.pswrd.value;

    if(auth_token == null) {
        $.ajax({
                type: 'POST',
                data: {
                username: userid,
                password: passwd
                },
                url: 'http://127.0.0.1:8000/api/v1/api-token-auth/',
                success: function(res){
                        var tok = res.token
                        localStorage.setItem("token", JSON.stringify(tok));

                }});
    } else {
        $.ajax({
            type: 'POST',
            headers: {
                'Authorization':'token ' + JSON.parse(auth_token)
            },
            data: {
                quote_text: "Test Javascript with variable token pt 2",
                tags: '["test", "javascript"]'
            },
            url: 'http://127.0.0.1:8000/api/v1/test/',
            success: function(res){
                console.log(res)  //answer of api call.
                }
        });
    };
};

然而,如果我对用户名和密码(login.js)进行了硬编码,这将起作用:

function checkLogin(form) {
    var auth_token = localStorage.getItem("token");

    //hard coded variable content which works.
    var userid = "test";
    var passwd = "test1";

    if(auth_token == null) {
        $.ajax({
                type: 'POST',
                data: {
                username: userid,
                password: passwd
                },
                url: 'http://127.0.0.1:8000/api/v1/api-token-auth/',
                success: function(res){
                        var tok = res.token
                        localStorage.setItem("token", JSON.stringify(tok));

                }});
    } else {
        $.ajax({
            type: 'POST',
            headers: {
                'Authorization':'token ' + JSON.parse(auth_token)
            },
            data: {
                quote_text: "Test Javascript with variable token pt 2",
                tags: '["test", "javascript"]'
            },
            url: 'http://127.0.0.1:8000/api/v1/test/',
            success: function(res){
                console.log(res)  //answer of api call.
                }
        });
    };
};

1 个答案:

答案 0 :(得分:-1)

关闭并重新打开浏览器。不知道为什么那是必要的。