使用jQuery ajax添加多个数据

我可以通过定位表单类< form class =“my_form_class”>来处理我的表单值:

jQuery.ajax({
        type:"GET",url: "/wp-admin/admin-ajax.php",data: my_form_class,context: this,success:function(data){

            // do stuff
        }
});

这非常有效.

但我想添加更多数据,所以我试过:

data: { my_form_class,security : 'foo' },

那没起效.我在这做错了什么?我试过了:

data: { my_form_class : my_form_class,

它显然也无效.

最佳答案
根据jQuery ajax的定义

data

Type: PlainObject or String or Array
Data to be sent to the server. It is converted to a query string,if not already a string. It’s appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array,jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

您可以使用jquery paramjQuery serialize

$('.my_form_class').serialize()  + '&' + $.param({security : 'foo'});

我的片段:

$(function () {
  $('#btn').on('click',function(e) {
    console.log($('.my_form_class').serialize()  + '&' + $.param({security : 'foo'}));
    $.ajax({
      type:"GET",data: $('.my_form_class').serialize()  + '&' + $.param({security : 'foo'}),success:function(data){
        // do stuff
      }
    }).fail(function(jqXHR,textStatus,errorThrown) {
      console.log('ajax error: ' + textStatus)
    });
  });
});