使用XMLHttpRequest将大型数组从Javascript发布到PHP

时间:2012-06-17 20:11:44

标签: php javascript post xmlhttprequest

我正在尝试通过PHP将大型数组从Javascript发送到mySQL。我正在尝试POST,但以下不起作用。 http.onreadystatechange中的'alert'弹出打开,似乎只包含下面的代码。代码看起来非常简单。任何帮助表示赞赏。

<?php 

if (isset($_POST['params'])) {
echo 'YES isset';
$phpobj = ($_POST['params']);
echo $phpobj;

}else{
echo 'No isset';
}

?>


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
http=new XMLHttpRequest();
}else{// code for IE6, IE5
http=new ActiveXObject("Microsoft.XMLHTTP");
}

var url = "PostArray.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
    alert(http.responseText);
}
}
http.send(params);

</script>

1 个答案:

答案 0 :(得分:-1)

  1. 在您要传递的数据中没有名为params的变量,只有lorem和name
  2. 你甚至没有发帖,因为你没有将变量params(在javascript中)传递给http对象