将变量传递给ajax中的URL

时间:2017-01-20 16:46:39

标签: javascript jquery ajax

我有下面的ajax调用,我试图将注册数据直接发布到URL,但无论如何我可以存储这些变量,然后将其传递给URL。请提前帮助你。

您可以在此plunker

中看到Script.js

jquery的:

$(function() {

    /* Registration form for the website */
    /* validation */
    $("#register-form").validate({
        rules: {
            firstName: {
                required: true
            },
            lastName: {
                required: true
            },
            userName: {
                required: true,
                minlength: 4
            },
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 8,
                maxlength: 15
            },
            cpassword: {
                required: true,
                equalTo: '#password'
            },
        },
        messages: {
            userName: "please enter a valid user name",
            password: {
                required: "please provide a password",
                minlength: "password at least have 8 characters"
            },
            email: "please enter a valid email address",
            cpassword: {
                required: "please retype your password",
                equalTo: "password doesn't match !"
            }
        },
        submitHandler: submitForm
    });
    /* validation */

    /* form submit */
    function submitForm() {
        var data = $("#register-form").serialize();
        // var data={
        //  firstName: $('#firstName').val(),
        // }

        $.ajax({

            url: 'http://localhost:8000?userName=&password=&firstName=&lastName=&email=',
            type: 'POST',
            data: data,

            beforeSend: function() {
                $("#error").fadeOut();
                $("#btn-submit").html('<span class="glyphicon glyphicon-transfer"></span> &nbsp; sending ...');
            },

            success: function(data) {
                if (data === 0) {

                    $("#error").fadeIn(1000, function() {


                        $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; Sorry email already taken !</div>');

                        $("#btn-submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account');

                    });

                } else if (data == 1) {

                    $("#btn-submit").html('<img src="btn-ajax-loader.gif" /> &nbsp; Signing Up ...');

                } else {

                    $("#error").fadeIn(1000, function() {

                        $("#error").html('<div class="alert alert-danger"><span class="glyphicon glyphicon-info-sign"></span> &nbsp; ' + data + ' !</div>');

                        $("#btn-submit").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Create Account');

                    });

                }
            }
        });
        return false;
    }
});

2 个答案:

答案 0 :(得分:1)

    url: 'http://localhost:8000?userName=&password=&firstName=&lastName=&email=',
    type: 'POST',
    data: data,

成为

    url: 'http://localhost:8000?' + data,
    type: 'GET',
    //data: data,

答案 1 :(得分:0)

您的AJAX网址错误,您无需输入完整地址。 Serialize方法应该为您做到这一点。您所需要的只是localhost:8000? + data。正如@ zer00ne所说,如果您输入密码,则永远不会使用GET,它会显示在网址上。根据经验,表格中发送的任何内容都应该是POST请求,除非是一些我不知道的罕见情况。

我的建议:

$.ajax({

        url: 'http://localhost:8000?',
        type: 'POST',
        data: data,

这样您就可以安全地将信息发送到正确的位置。