简体   繁体   English

jQuery,Ajax和PHP发行给isset方法

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

I' ve a issue that I could not solve, I've read a lot of questions similar to my, but have not been able to solve. 我有一个我无法解决的问题,我已经阅读了很多与我类似的问题,但未能解决。

This is my code: 这是我的代码:

Ajax call: 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 code is: 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 is always false, why? Isset总是假的,为什么呢?

I've tried also with GET instead of POST, but nothing changed. 我也尝试过使用GET而不是POST,但是没有任何改变。

Thanks. 谢谢。

You have ajax.done() going to download.php , this means when download.php get POST value from ajax, and it succeed, you didn't take it's response back but redirect to download.php, when it redirect to download.php it doesn't POST or GET anything, means it will always show isset() false, for "REDIRECTED" part. 您有ajax.done()download.php ajax.done() ,这意味着,当download.php从ajax获取POST值时,它成功了,您没有将响应返回,而是重定向到download.php,当它重定向到download时。 PHP不POSTGET什么,意味着它会一直显示isset()假,对于“REDIRECTED”的一部分。 replace your window.location with alert() and I am sure you will get your values posted. 将您的window.location替换为alert(),我相信您会发布您的值。

change datatype for ajax function 更改ajax函数的数据类型

dataType: "html" or remove dataType option dataType:“ html”或删除dataType选项

doctype is a reserved keyword in javascript. doctype是javascript中的保留关键字。

Write it as 'doctype' : 将其写为“ doctype”:

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

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