how to send multiple data with $.ajax() jquery

后端 未结 9 1523
臣服心动
臣服心动 2020-11-27 03:07

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 i

相关标签:
9条回答
  • 2020-11-27 03:23

    You can create an object of key/value pairs and jQuery will do the rest for you:

    $.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

    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)
    
    0 讨论(0)
  • 2020-11-27 03:23

    you can use FormData

    take look at my snippet from 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) {},
    
    0 讨论(0)
  • 2020-11-27 03:25
    var data = 'id='+ id  & 'name='+ name;
    

    The ampersand needs to be quoted as well:

    var data = 'id='+ id  + '&name='+ name;
    
    0 讨论(0)
  • 2020-11-27 03:27
    var CommentData= "u_id=" + $(this).attr("u_id") + "&post_id=" + $(this).attr("p_id") + "&comment=" + $(this).val();
    
    0 讨论(0)
  • 2020-11-27 03:33
    var value1=$("id1").val();
    var value2=$("id2").val();
    data:"{'data1':'"+value1+"','data2':'"+value2+"'}"
    
    0 讨论(0)
  • 2020-11-27 03:34

    Change var data = 'id='+ id & 'name='+ name; as below,

    use this instead.....

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

    this will going to work fine:)

    0 讨论(0)
提交回复
热议问题