我正在使用 jQuery 在 POST 请求上传递一个数组,但是我不明白如何使用“vanilla”javascript 执行相同的操作。这是我的 jQuery:
// Request using jQuery
$.ajax({type:'POST',url:'insert-user.php',data: {myArray: myArray},
success:function(data_response){
console.log("jQuery, got data back, response: "+data_response);
}});
这就是我目前尝试使用纯 js 实现的方式:
// Request using plain js
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
console.log("JS, got data back, response: "+xmlhttp.responseText);
}
}
xmlhttp.open("POST","insert-user.php",true);
xmlhttp.send({myArray: myArray});
最佳答案
使用这个:
xmlhttp.send('myArray='+JSON.stringify(myArray));
在 php 端,
$array= json_decode($_POST['myArray']);
关于javascript - 使用普通 js 在 AJAX 请求上传递数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19429286/