开发者

jQuery post dynamic keys via ajax

开发者 https://www.devze.com 2023-04-03 03:16 出处:网络
I have an array which contains many keys/values that are generated dynamically, so I never know what data I am definitely posting. This means I can\'t use :

I have an array which contains many keys/values that are generated dynamically, so I never know what data I am definitely posting. This means I can't use :

$.ajax({
    url: "php/addressMain.php", 
    data: {data: alldata},
    type: "POST",
    success: function(data) {}
});

As I never know what data will be. I am currently just converting it to a querystring and posting it as a GET, however 开发者_StackOverflow中文版what I really need is something that will convert my array to a data array for the AJAX post, as the querystring becomes too long.


Since you've already been able to create a querystring, why not use the query string with POST since your issues seems to be the length limitation of GET?


I think $(...).serialize() will be able to help you.


I'm not sure I follow what the issue is, but this should work for passing data.

var POSTdata = [1,2,3,4];
$.ajax({
  url: "php/addressMain.php", 
  data: { data: POSTdata },
  type: "POST",
  success: function(data) {}
});

Then in PHP you should have this:

$_POST['data'] 
// should be = array( 1, 2, 3, 4)

The same is true if POSTdata = {key: value, key2: value2} Then $_POST['data'] == array( 'key' => 'value', 'key2' => 'value2' )

0

精彩评论

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

关注公众号