Variables in jQuery Ajax POST request
I'm trying to use jQuery's $.post()
method with variables from values taken from textboxes on the page. My code looks like this:
$.post("install-ajax.php"开发者_运维百科, { host: $("#host").val(), user: $("#user").val(), pass: $("#pass").val(), name: $("#name").val() }, function(data) {
$("#post").append(data);
});
So far, the request hasn't worked at all. Is this possible to do? If so, what do I have to do to make it work?
Thanks very much in advance! :)
Judging from the variables that you want to send via Ajax, I'd suggest trying the following:
function construct_vars_from_ids(vars) {
var obj = {};
jQuery.each(vars, function(index, el) {
obj[el] = $('#' + el).val();
});
return obj;
}
$.post("install-ajax.php", construct_vars_from_ids(['host', 'user', 'pass', 'name']), function(data) {
$("#post").append(data);
});
精彩评论