更新
我使用另一种解决方案将数据写入文件。当AJAX等待响应时,我似乎无法回显数据。所以我现在使用fwrite。
$fileHandle = '';
$fileHandle = fopen("export.txt","w");
fwrite($fileHandle, $export);
原始
你好, 也许我的逻辑错了。 我进行了一次AJAX调用以从另一个URL获取数据。 到目前为止,这是有效的。
但现在我想添加一个文件导出。
$handler = new MyHandler();
// Step 1: get data from URL
$dataAjax = $handler->getData($_POST['data']);
// Step 2: write the data into a text file to provide a download
$handler->writeToText($dataAjax);
echo json_encode($dataAjax);
现在控制台显示“parserError”,因为我的JSON数据还包含我想写入文件的字符串。那是坏事和不必要的。 以下是测试我如何将数据写入txt文件的测试:
function writeToText($data)
{
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=export.txt");
header("Pragma: no-cache");
header("Expires: 0");
$title = "";
$title .= "Name,Quantity,Model,Price,Weight,Status"."\n";
echo $title;
}
这就是错误的样子:
{
"readyState": 4,
"responseText": "Name,Quantity,Model,Price,Weight,Status\n[{\"domain\":\"Text\",\"name\":\"Banana\}]",
"status": 200,
"statusText": "OK"
}
parsererror