2017-01-20 124 views
-1

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

你可以看到的script.js这个plunker

的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; 
    } 
}); 
+0

可能的重复[如何检查数据是否通过ajax](http://stackoverflow.com/questions/41766927/how-to-check-if-data-is-passed-in-ajax) –

+0

如果你想把变量放到URL中,然后使用'GET'类型的请求。 – Black

+0

@Leo这是一个重复我同意,但这是我有不同的问题。我能够看到数据,但没有正确传递给URL,出于某种原因,导致所有输入的'逗号'。我认为这是因为我已经将它硬编码到URL本身,而不是存储每个单独的组件,然后将它传递给URL。你能帮助一个例子吗? – Bob

回答

0

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

我的建议:

$.ajax({ 

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

这样你发送你的信息传递到正确的地方和安全。

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

成为

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

,因为我们不希望我们的密码对像zer00ne所说的任何人都可见。所以我会坚持利奥的解决方案。 – Bob