javascript - jQuery 将变量发布到 php 并下载文件

标签 javascript php jquery

我不确定我的代码是否正确,但我不断收到错误消息,指出调试器中不允许 post

我正在尝试将我的 javascript 变量 - 下载网址和新文件名 - 发送到我的 php 文件,然后使用 php 下载 .mp3。

jQuery:

$.post('https://scriptkitti.github.io/Articles/Files/DownCloud.php', {
    'filePath': url + '?client_id=' + client,
    'fileTitle': title
});

PHP:

<?php
    if (isset($_POST['fileTitle'], $_POST['filePath'])) {
        $fileTitle = $_POST['fileTitle'];
        $filePath = $_POST['filePath'];
        header('Content-Type: audio/mp3');
        header('Content-Disposition: attachment; filename="' . $fileTitle . '.mp3";');
        header('Pragma: public');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Length: ' . filesize($filePath));
        readfile($filePath);
        exit;
    }
?>

最佳答案

您发布的链接未执行。我去了它,它下载了 php 源代码。据此answer ,github页面不执行服务器端代码(php),它仅用于静态内容(html,css,js等)

关于javascript - jQuery 将变量发布到 php 并下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33574487/

相关文章:

php - 从 Jquery 获取 PHP $_SESSION 变量?

javascript - 正则匹配表情

javascript - TensorFlow.js 返回类型错误 : Cannot read property 'concat' of undefined when loading models

javascript - 在 jQuery 中的 document.ready 中分配的 click()

php - 当我点击一个菜单元素时,想在我的当前页面中用 href 打开一个页面

php - 在cakephp中访问相关模型/表进行内连接查询

javascript - 替换字符串javascript中的最后一个字符

php - Else 子句不触发检查 mysql 结果

jquery - 将 'touchstart' 绑定(bind)到文档时的 Mobile Safari (iPad) 滚动性能

javascript - 停止在表单中发布隐藏的选择字段