我正在使用 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});