开发者

problem with sending array with jquery ajax

开发者 https://www.devze.com 2023-03-12 12:30 出处:网络
var myNames= [\"Chris\",\"Kate\",\"Steve\"]; $.ajax ({ cache:false, type: \"POST\", url: \"check.php?timestamp=\"+new Date().getTime(),
var myNames= ["Chris","Kate","Steve"];
$.ajax
({
cache:false,
    type: "POST",
    url: "check.php?timestamp="+new Date().getTime(),
    data: "myCars[]=开发者_运维技巧"+myCars,
    success: function(msg)
         {
 ...
         }
});

with var_dump($myNames) in php page i see

array(1) { [0]=> string(16) "Chris,Kate,Steve" }

but i expect

array(3) {....}

why i see array(1) { [0]=> string(16) "Chris,Kate,Steve" }


There's a better way of doing this - just pass an object for data:

var myNames= ["Chris","Kate","Steve"];
$.ajax
({
cache:false,
    type: "POST",
    url: "check.php?timestamp="+new Date().getTime(),
    data: {'myNames': myNames},
    success: function(msg)
         {
 ...
         }
});

jQuery will encode it for you.

0

精彩评论

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

关注公众号