【发布时间】:2016-03-19 21:16:16
【问题描述】:
我想用 ajax 向 php 发送值,将值保存在 txt 文件中,并为用户提供保存文件的选项。
我收到错误消息:
警告:无法修改标头信息 - 标头已由 /Applications/MAMP/ 中的(从 /Applications/MAMP/htdocs/randomColors/webroot/incl/theme.php:26 开始的输出)发送htdocs/randomColors/webroot/palettes.php 第 26 行
我做错了什么?
function exportColors() {
$.ajax({
type: "POST",
url: "palettes.php",
data: ({data: 'John'}),
success: function (data) {
}
});
}
这是export.php中的代码:
if (isset($_POST['data']))
{
$handle = fopen("file.txt", "w");
fwrite($handle, $_POST['data']);
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
}
涉及的html是一个div:
<div class="palettesDIV" data-id="220">
当它被点击时,“data-id”中的值将被提交到 export.php 与 exportColors 功能。现在我只使用 {data: 'John'} 作为占位符。
exportColors 函数由这段代码触发(放置在 for 循环中)
palettesDIVArray[x].addEventListener('click', exportColors, false);
【问题讨论】:
-
您在
$_POST['data']中拥有的内容您是否尝试过转储它?在export.php写var_dump( $_POST['data'] )看看它会给你什么 -
能否请您发布用于发布数据的表单?您的错误表明您的 $_POST 数组中没有任何名为“数据”的索引。
-
为什么要导航到 export.php `window.location.replace("export.php");`?你的反应对吗?当你调用它时,你的帖子数据是空的,所以返回它
-
如果你要去export.php,为什么不直接使用php post?
-
我已对其进行了更改,因此我不会导航到 export.php,而是将其发送到相同/当前页面 (palettes.php)。我还删除了代码 window.location.replace("export.php");