将二维数组从JavaScript发送到php,反之亦然
我使用此代码发送整数的一维数组,但如何使其发送和接收整数和字符串组合形成的二维数组,例如[number here][“text here”],但url有一个限制,因此我无法生成大数组将二维数组从JavaScript发送到php,反之亦然,php,javascript,json,Php,Javascript,Json,我使用此代码发送整数的一维数组,但如何使其发送和接收整数和字符串组合形成的二维数组,例如[number here][“text here”],但url有一个限制,因此我无法生成大数组 //Send data to php var queryString ="?"; for(var t=0;t<alldays.length;t++) {
//Send data to php
var queryString ="?";
for(var t=0;t<alldays.length;t++)
{
if(t==alldays.length-1)
queryString+="arr[]="+alldays[t];
else
queryString+="arr[]="+alldays[t]+"&";
}
ajaxRequest.open("POST", "forbidden.php" + queryString, true);
ajaxRequest.send(null);
}
// Create a function that will receive data sent from the server(sended as echo json_encode($array))
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var myArray = ajaxRequest.responseText.replace("[","").replace("]","").replace(/"/g,"").split(",");
for(var i=0; i<myArray.length; i++) { alldays[i] = parseInt(myArray[i]); }
}}
//将数据发送到php
var queryString=“?”;
对于(var t=0;t,不要将请求作为添加到url的查询字符串包含在内,而是将其作为帖子正文发送:
var queryString ="";
for(var t=0;t<alldays.length;t++)
{
if(t==alldays.length-1)
queryString+="arr[]="+alldays[t];
else
queryString+="arr[]="+alldays[t]+"&";
}
ajaxRequest.open("POST", "forbidden.php", true);
ajaxRequest.send(queryString);
然后在服务器上,您可以使用$\u POST[“n”]
检索数组元素的数量,然后处理$\u POST[“arr”+t]
从0到n-1
的每个t
的数组元素。您确实使用了POST
请求-因此不要在获取查询字符串的参数中发送数组
var queryString =""; // no question mark
// construct rest of query string here
ajaxRequest.open("POST", "forbidden.php", true);
ajaxRequest.send(queryString);
另外,对响应使用JSON。PHP端:,JavaScript端:@user1874258-您的帖子名称都命名为“arr[]”
,没有一个命名为“arr”
。我添加了处理此问题的建议。是响应还是接受答案?
var queryString =""; // no question mark
// construct rest of query string here
ajaxRequest.open("POST", "forbidden.php", true);
ajaxRequest.send(queryString);