我有一个 php 脚本,可以创建用户特定的 pdf 文件。 (mpdf)
为了下载文件而不丢失当前页面,我使用了 ajax。
var jsonString = JSON.stringify(multydimarray);
$.ajax({
type: "POST",
url: "gen.php",
data: {data : jsonString}
success: function(response){
window.location = "gen.php";
}
});
但是 gen.php 没有收到来自 ajax 的任何数据。 $_POST['data'] 未设置所以,
文件已下载,当前页面保持不变,但文件为空。
有什么建议吗?
最佳答案
指定ajax请求的contentType为application/json;字符集=utf-8
$.ajax({
type: "POST",
url: "gen.php",
data: {data : jsonString}
contentType: "application/json; charset=utf-8",
success: function(response){
window.location = "gen.php";
}
});
然后像这样在 PHP 中获取数据
$data = json_decode(file_get_contents('php://input'));
关于javascript - 使用ajax打开动态php页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24150450/