我有下面的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> 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> Sorry email already taken !</div>');
$("#btn-submit").html('<span class="glyphicon glyphicon-log-in"></span> Create Account');
});
} else if (data == 1) {
$("#btn-submit").html('<img src="btn-ajax-loader.gif" /> Signing Up ...');
} else {
$("#error").fadeIn(1000, function() {
$("#error").html('<div class="alert alert-danger"><span class="glyphicon glyphicon-info-sign"></span> ' + data + ' !</div>');
$("#btn-submit").html('<span class="glyphicon glyphicon-log-in"></span> Create Account');
});
}
}
});
return false;
}
});
可能的重複[如何檢查數據是否通過ajax](http://stackoverflow.com/questions/41766927/how-to-check-if-data-is-passed-in-ajax) –
如果你想把變量放到URL中,然後使用'GET'類型的請求。 – Black
@Leo這是一個重複我同意,但這是我有不同的問題。我能夠看到數據,但沒有正確傳遞給URL,出於某種原因,導致所有輸入的'逗號'。我認爲這是因爲我已經將它硬編碼到URL本身,而不是存儲每個單獨的組件,然後將它傳遞給URL。你能幫助一個例子嗎? – Bob