我想发送一个数组作为Ajax请求:
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
我该怎么做呢?
我想发送一个数组作为Ajax请求:
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
我该怎么做呢?
当前回答
info = [];
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
data: {info:info},
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
其他回答
info = [];
info[0] = 'hi';
info[1] = 'hello';
$.ajax({
type: "POST",
data: {info:info},
url: "index.php",
success: function(msg){
$('.answer').html(msg);
}
});
注意:不适用于更新版本的jQuery。
因为你正在使用jQuery,请使用它的seralize函数来序列化数据,然后将其传递到ajax调用的data参数:
info[0] = 'hi';
info[1] = 'hello';
var data_to_send = $.serialize(info);
$.ajax({
type: "POST",
url: "index.php",
data: data_to_send,
success: function(msg){
$('.answer').html(msg);
}
});
只需使用JSON。方法,并将其作为$. Stringify的"data"参数传递。Ajax函数,如下:
$.ajax({
type: "POST",
url: "index.php",
dataType: "json",
data: JSON.stringify({ paramName: info }),
success: function(msg){
$('.answer').html(msg);
}
});
你只需要确保在你的页面中包含JSON2.js文件…