如何使用 $.ajax() jquery 发送多个数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10078085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send multiple data with $.ajax() jquery
提问by sohaan
i am trying to send multiple data using j query $.ajax method to my php script but i can pass only single data when i concatenate multiple data i get undefined index error in my php script tat means the ajax request is made but data is not sent i need to know how should i format multiple data to successively send it to processing script in name vale pair here is what i have written
我正在尝试使用 j 查询 $.ajax 方法将多个数据发送到我的 php 脚本,但是当我连接多个数据时我只能传递单个数据我在我的 php 脚本中收到未定义的索引错误 tat 意味着 ajax 请求已发出但数据不是发送我需要知道我应该如何格式化多个数据以将其连续发送到名称值对中的处理脚本这里是我写的
<script>
$(document).ready(function() {
$('#add').click(function () {
var name = $('#add').attr("data_id");
var id = $('#add').attr("uid");
var data = 'id='+ id & 'name='+ name; // this where i add multiple data using ' & '
$.ajax({
type:"GET",
cache:false,
url:"welcome.php",
data:data, // multiple data sent using ajax
success: function (html) {
$('#add').val('data sent sent');
$('#msg').html(html);
}
});
return false;
});
});
</script>
<span>
<input type="button" class="gray_button" value="send data" id="add" data_id="1234" uid="4567" />
</span>
<span id="msg"></span>
回答by Jasper
You can create an object of key/value pairs and jQuery will do the rest for you:
您可以创建一个键/值对对象,jQuery 将为您完成剩下的工作:
$.ajax({
...
data : { foo : 'bar', bar : 'foo' },
...
});
This way the data will be properly encoded automatically. If you do want to concoct you own string then make sure to use encodeURIComponent()
: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
这样,数据将自动正确编码。如果您确实想编造自己的字符串,请确保使用encodeURIComponent()
:https: //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
Your current code is not working because the string is not concocted properly:
您当前的代码无法正常工作,因为该字符串未正确编造:
'id='+ id & 'name='+ name
should be:
应该:
'id='+ encodeURIComponent(id) + '&name='+ encodeURIComponent(name)
回答by Selvakumar Arumugam
Change var data = 'id='+ id & 'name='+ name;
as below,
更改var data = 'id='+ id & 'name='+ name;
如下,
use this instead.....
用这个代替.....
var data = "id="+ id + "&name=" + name;
this will going to work fine:)
这将正常工作:)
回答by xbonez
var data = 'id='+ id & 'name='+ name;
The ampersand needs to be quoted as well:
与号也需要引用:
var data = 'id='+ id + '&name='+ name;
回答by Simon Steinberger
I would recommend using a hash instead of a param string:
我建议使用散列而不是参数字符串:
data = {id: id, name: name}
回答by hadi.sh
var value1=$("id1").val();
var value2=$("id2").val();
data:"{'data1':'"+value1+"','data2':'"+value2+"'}"
回答by Chad
var my_arr = new Array(listingID, site_click, browser, dimension);
var AjaxURL = 'http://example.com';
var jsonString = JSON.stringify(my_arr);
$.ajax({
type: "POST",
url: AjaxURL,
data: {data: jsonString},
success: function(result) {
window.console.log('Successful');
}
});
This has been working for me for quite some time.
这已经为我工作了一段时间。
回答by Basheer AL-MOMANI
you can use FormData
您可以使用 FormData
take look at my snippet from MVC
看看我的 MVC 片段
var fd = new FormData();
fd.append("ProfilePicture", $("#mydropzone")[0].files[0]);// getting value from form feleds
d.append("id", @(((User) Session["User"]).ID));// getting value from session
$.ajax({
url: '@Url.Action("ChangeUserPicture", "User")',
dataType: "json",
data: fd,//here is your data
processData: false,
contentType: false,
type: 'post',
success: function(data) {},
回答by Badshah Sahib
var CommentData= "u_id=" + $(this).attr("u_id") + "&post_id=" + $(this).attr("p_id") + "&comment=" + $(this).val();
回答by chamina
var value1=$("id1").val();
var value2=$("id2").val();
data:"{'data1':'"+value1+"','data2':'"+value2+"'}"
You can use this way to pass data
可以用这种方式传递数据