我正在通过 Jquery Ajax 帖子发送数据,我想从我的表单中发送文本框,还有一个名为 username 的变量。例如...
$.ajax({type:'POST', url: 'submit.php', data:$('#myform').serialize(), username});
我怎样才能做到这一点?
我正在通过 Jquery Ajax 帖子发送数据,我想从我的表单中发送文本框,还有一个名为 username 的变量。例如...
$.ajax({type:'POST', url: 'submit.php', data:$('#myform').serialize(), username});
我怎样才能做到这一点?
只需将额外参数添加到序列化字符串
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize()+'&username='+encodeURIComponent(username)
});
As a good practice, it is better to construct a data object and also include the extra variables. Try constructing a data object and include them ?
data = {
'formData': $('#myform').serialize(),
'variable': 'username'
}
You can pass it your other variable via get like this.
$.ajax({type:'POST', url: 'submit.php?myvar='+encodeURIComponent(myvar), data:$('#myform').serialize(), username});
You could also add the value to a hidden field in your form.
尝试这个
$.ajax({
type:'POST',
url: 'submit.php',
data: { form: $('#myform').serialize(), username: encodeURIComponent(username)}
});
检查ajax 文档将数据发送到服务器的部分
试试下面的查询。用户名应该是一个 js 变量。
var username = "myname";
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize(),
username:username
});
尝试,
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize(),
username: username //you missed the key
});