繁体   English   中英

jQuery,Ajax和PHP发行给isset方法

[英]JQuery, Ajax and PHP issue to isset method

我有一个我无法解决的问题,我已经阅读了很多与我类似的问题,但未能解决。

这是我的代码:

Ajax呼叫:

function download() {           
    var doctypeString = $('#doctypeComponent').attr('class')+"";
    var metaString = $('#metaComponent').attr('class')+"";
    var cssString = $('#cssComponent').attr('class')+"";
    var jsString = $('#jsComponent').attr('class')+"";
    var ajax = $.ajax({
        type: 'POST',
        url: 'php/download.php',
        data: {doctype : doctypeString, meta : metaString, css : cssString, js :  jsString},
        dataType: "text"
    });

    ajax.done(function() {
        window.location = 'php/download.php';           
    });

    ajax.fail(function(jqXHR, error) {
        alert("Request failed: "+error);
    });

}

php代码是:

if(isset($_POST['doctype'])){
    $doctype = $_POST['doctype'];
    writeFile($doctype);
} else {
    $doctype = "error doctype\n";
    writeFile($doctype);
}

if(isset($_POST['meta'])){
    $meta = $_POST['meta'];
    writeFile($meta);
} else {
    $meta = "error meta\n";
    writeFile($meta);
}

if(isset($_POST['css'])){
    $css = $_POST['css'];
    writeFile($css);
} else {
    $css = "error css\n";
    writeFile($css);
}

if(isset($_POST['js'])){
    $js = $_POST['js'];
    writeFile($js);
} else {
    $js = "error js\n";
    writeFile($js);
}

$file = $file."\n\t</head>\n\t<body>\n\t</body>\n</html>";

downloadFile("test.html");

function writeFile($content) {
    global $file;
    $file = $file.$content."";
}

function downloadFile($filename) {
    global $file; 
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-disposition: attachment; filename="'.basename($filename).'";');
    header('Content-Length: '.strlen($file));
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Expires: 0');
    header('Pragma: public');
    echo $file;
    exit;   
}

Isset总是假的,为什么呢?

我也尝试过使用GET而不是POST,但是没有任何改变。

谢谢。

您有ajax.done()download.php ajax.done() ,这意味着,当download.php从ajax获取POST值时,它成功了,您没有将响应返回,而是重定向到download.php,当它重定向到download时。 PHP不POSTGET什么,意味着它会一直显示isset()假,对于“REDIRECTED”的一部分。 将您的window.location替换为alert(),我相信您会发布您的值。

更改ajax函数的数据类型

dataType:“ html”或删除dataType选项

doctype是javascript中的保留关键字。

将其写为“ doctype”:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM