【问题标题】:POST large array from Javascript to PHP with XMLHttpRequest使用 XMLHttpRequest 将大型数组从 Javascript 发布到 PHP
【发布时间】:2012-06-19 22:13:46
【问题描述】:

我正在尝试通过 PHP 将一个大型数组从 Javascript 发送到 mySQL。我正在尝试使用 POST,但以下方法不起作用。 http.onreadystatechange 中的“警报”弹出并似乎只包含下面的代码。代码看起来很简单。任何帮助表示赞赏。

<?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>

【问题讨论】:

  • 您不使用像 jQuery 这样的库的任何特殊原因?
  • 谢谢肖恩。我想我会尝试 JQuery
  • 嗨,代码似乎正确。我建议您使用 Chrome 及其控制台来检查通话,看看您的浏览器是否真的在发布您的数据。如果是这种情况,您现在会发现问题可能出在 php 方面。

标签: php javascript post xmlhttprequest


【解决方案1】:
  1. 在您要传递的数据中没有名为 params 的变量,只有 lorem 和 name
  2. 您甚至没有发布该内容,因为您还没有将变量 params(在 javascript 中)传递给 http 对象

【讨论】:

    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 2011-11-03
    • 2014-08-16
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    相关资源
    最近更新 更多