【发布时间】:2016-02-05 03:41:18
【问题描述】:
几天来,我一直在尝试让 ajax 警报层与 POST 方法一起使用,但我想不出它不工作的原因。我使用相同的基本代码通过 ajax 在其他管理页面上使用 POST 发送表单数据,但当我尝试发送不是来自表单的数据时,$_POST 中没有任何东西到达服务器。
下面是代码流程...
我在这样的页面上使用变量:
$alertLayer = 1;
$autoCloseAlertLayer = 1;
$addAlertLayerCloseButton = 1;
$alertLayerMessage = $alertLayerMessage . '<h1>Test</h1><p>3rd test of the alert layer module.</p>';
$redirect = 0;
$redirectTo = 0;
我在页面底部包含一个调用函数的脚本,如下所示:
if ($alertLayer == true)
{
echo "<script type='text/javascript' id='alertLayerScript'>Lib.ajaxAlertFunction('/Modules/AlertLayer', $autoCloseAlertLayer, $addAlertLayerCloseButton, '$alertLayerMessage', $redirect, '$redirectTo');</script>";
}
这是被调用的脚本:
Lib.ajaxAlertFunction = function (senturl, autoClose, closeButton, message, redirect, redirectTo)
{
var ajaxRequest;
try
{
ajaxRequest = new XMLHttpRequest();
}
catch (e)
{
try
{
ajaxRequest = new ActiveXObjext("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
ajaxRequest = new ActiveXObjext("Microsoft.XMLHTTP");
}
catch (e)
{
alert ("Your browser can't handle the truth!");
return false;
}
}
}
if (!senturl)
{
return false;
}
else
{
// var data = "autoClose=" + encodeURIComponent(autoClose) + "&closeButton=" + encodeURIComponent(closeButton) + "&message=" + encodeURIComponent(message) + "&redirect=" + encodeURIComponent(redirect) + "&redirectTo=" + encodeURIComponent(redirectTo);
// var data = encodeURIComponent("autoClose=" + autoClose + "&closeButton=" + closeButton + "&message=" + message + "&redirect=" + redirect + "&redirectTo=" + redirectTo);
var data = "autoClose=" + autoClose + "&closeButton=" + closeButton + "&message=" + message + "&redirect=" + redirect + "&redirectTo=" + redirectTo;
}
ajaxRequest.onreadystatechange = function()
{
if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200)
{
document.getElementById('outerFrame').innerHTML += ajaxRequest.responseText;
newAlertLayer = document.getElementById('alertLayer');
var arr = newAlertLayer.getElementsByTagName('script')
for (var n = 0; n < arr.length; n++)
{
eval(arr[n].innerHTML)
}
}
}
ajaxRequest.open('POST', senturl, true);
ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
ajaxRequest.send(data);
}
注意:我使用 'GET' 方法发送此数据没有问题,但随后一条长消息被截断。我还尝试使用过去 3 天搜索的几种不同方法设置“数据”变量,但均未成功。
需要 $_POST 数据的代码如下:
<?php
$ROOT = $_SERVER['DOCUMENT_ROOT'];
?>
<div id="alertLayer">
<link rel="stylesheet" href="<?php $ROOT ?>/Modules/AlertLayer/alertLayer.css">
<script src="/Modules/AlertLayer/alertLayer.js"></script>
<div id="alertBlock">
<?php
foreach ($_POST as $key => $value)
{
echo "<p>" . $key . " = " . $value . "</p>";
}
foreach ($_GET as $key => $value)
{
echo "<p>" . $key . " = " . $value . "</p>";
}
?>
</div>
</div>
我错过了什么?与使用 POST 发送表单数据和发送以相同方式连接的变量有什么不同? 同样,当我将数据添加到 url 字符串但不够时,GET 正在工作,POST = 在 ajaxRequest 的另一端根本没有收到任何数据,但请求的其余部分返回的正是预期的内容。服务器请求中缺少的 $_POST 数据是目前我无法使用此代码解决的唯一问题。
请求似乎未正确发送,但我无法确定原因。这是 chrome 中 NETWORK 选项卡的屏幕截图:
【问题讨论】:
-
尝试从数据中删除
encodeURIComponent....就像你说的,它会被截断 - 没有它似乎很好。 -
我刚刚编辑了上面的代码以显示我最初开始使用的内容,然后再尝试使用 encodeURIComponent 和其他方法。仅使用连接在一起的字符串是我的问题开始的地方,我猜为什么我使用 GET 没有问题。
标签: javascript php ajax post