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

后端 未结 9 1662
臣服心动
臣服心动 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)
    

提交回复
热议问题