如何使用$ .ajax()jQuery发送多个数据

我正在尝试使用j查询$ .ajax方法向我的php脚本发送多个数据,但是当我连接多个数据时我只能传递单个数据我的php脚本tat中出现未定义的索引错误,意味着发出了ajax请求,但数据不是 发送,我需要知道如何格式化多个数据以依次将其发送到名称为vale对的处理脚本中,这就是我写的内容

<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>
sohaan asked 2020-07-30T19:12:10Z
9个解决方案
91 votes

您可以创建一个键/值对的对象,jQuery将为您完成其余工作:

$.ajax({
    ...
    data : { foo : 'bar', bar : 'foo' },
    ...
});

这样,数据将被自动正确编码。 如果您确实想编造自己的字符串,请确保使用encodeURIComponent():[https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent]

您当前的代码无法正常工作,因为该字符串未正确编写:

'id='+ id  & 'name='+ name

应该:

'id='+ encodeURIComponent(id) + '&name='+ encodeURIComponent(name)
Jasper answered 2020-07-30T19:12:40Z
9 votes

如下更改var data = 'id='+ id & 'name='+ name;

用它代替.....

var data = "id="+ id + "&name=" + name;

这将正常工作:)

Selvakumar Arumugam answered 2020-07-30T19:13:08Z
6 votes
var data = 'id='+ id  & 'name='+ name;

还需引用“&”号:

var data = 'id='+ id  + '&name='+ name;
xbonez answered 2020-07-30T19:13:28Z
5 votes

我建议使用哈希而不是参数字符串:

data = {id: id, name: name}
Simon Steinberger answered 2020-07-30T19:13:48Z
5 votes
var value1=$("id1").val();
var value2=$("id2").val();
data:"{'data1':'"+value1+"','data2':'"+value2+"'}"
hadi.sh answered 2020-07-30T19:14:03Z
5 votes
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');
        }
    });

这已经为我工作了一段时间。

Chad answered 2020-07-30T19:14:23Z
1 votes

您可以使用FormData

看看我的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) {},
Basheer AL-MOMANI answered 2020-07-30T19:14:47Z
0 votes
var CommentData= "u_id=" + $(this).attr("u_id") + "&post_id=" + $(this).attr("p_id") + "&comment=" + $(this).val();
Badshah Sahib answered 2020-07-30T19:15:02Z
-1 votes
  var value1=$("id1").val();
  var value2=$("id2").val();
    data:"{'data1':'"+value1+"','data2':'"+value2+"'}"

您可以使用这种方式来传递数据

chamina answered 2020-07-30T19:15:22Z
translate from https://stackoverflow.com:/questions/10078085/how-to-send-multiple-data-with-ajax-jquery