[英]Send two pieces of data via Jquery AJAX?
I'm sending data via Jquery Ajax post, I want to send the text box from my form, but also a variable called username. 我通过Jquery Ajax发送数据,我想从我的表单发送文本框,但也是一个名为username的变量。 For example... 例如...
$.ajax({type:'POST', url: 'submit.php', data:$('#myform').serialize(), username});
How can I do this? 我怎样才能做到这一点?
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'
}
Just add the extra parameter to the serialized string 只需将额外参数添加到序列化字符串即可
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize()+'&username='+encodeURIComponent(username)
});
try the query below. 尝试下面的查询。 username should be a js variable. username应该是一个js变量。
var username = "myname";
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize(),
username:username
});
Try, 尝试,
$.ajax({
type:'POST',
url: 'submit.php',
data:$('#myform').serialize(),
username: username //you missed the key
});
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. 您还可以将值添加到表单中的隐藏字段。
Try this 尝试这个
$.ajax({
type:'POST',
url: 'submit.php',
data: { form: $('#myform').serialize(), username: encodeURIComponent(username)}
});
check ajax documentation the part Sending Data to the Server 检查ajax文档部分向服务器发送数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.