开发者

how to type ajax correctly?

开发者 https://www.devze.com 2023-01-27 14:28 出处:网络
i know how to use one form data use ajax: $.ajax({ type:\'get\', url:\'blabla.php\', data:$(\'#abc\').serialize();

i know how to use one form data use ajax:

$.ajax({
       type:'get',
       url:'blabla.php',
       data:$('#abc').serialize();
       .........

but how if i want to type:

$('#exportmod').click(function(){
                    $.ajax({
                            "dataType":'json',
                            "type":'GET',
                            "url":'shows_merc开发者_如何学Gohan.php',
                            "data": [ action:"searchmodelqp",
                                      jhead:"aaData",
                                      month:$("#search_month").val(),
                                      year:$("#search_year").val(),
                                      export:"excel"
                                    ],
                             "success":function(json){
                                                       fnCallback(json);
                                                       }
                             });
                    });

could you show me the correct type for this ajax?


You almost have it, just the bracing on your data is wrong, it should be {} rather than [] for an object, like this:

$('#exportmod').click(function(){
  $.ajax({
     dataType: 'json',
     type: 'GET',
     url: 'shows_merchan.php',
     data: { action: "searchmodelqp",
             jhead: "aaData",
             month: $("#search_month").val(),
             year: $("#search_year").val(),
             export: "excel"
           },
     success: fnCallback
   });
});

The other change above show what you can do. You don't have to quote identifiers for words that aren't reserved (as long as their valid...and all the $.ajax() options are), also there's no need for an anonymous wrapped to call a function with the same signature, so I changed success to use your callback directly.

There's also a shortcut for the above $.ajax() call, $.getJSON():

$('#exportmod').click(function(){
  $.getJSON('shows_merchan.php', 
            { action: "searchmodelqp",
              jhead: "aaData",
              month: $("#search_month").val(),
              year: $("#search_year").val(),
              export: "excel" }, 
            fnCallback);
});
0

精彩评论

暂无评论...
验证码 换一张
取 消