[英]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不POST
或GET
什么,意味着它會一直顯示isset()
假,對於“REDIRECTED”的一部分。 將您的window.location
替換為alert(),我相信您會發布您的值。
更改ajax函數的數據類型
dataType:“ html”或刪除dataType選項
doctype是javascript中的保留關鍵字。
將其寫為“ doctype”:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.