Javascript Ajax函数不调用php脚本
因此,我正在创建一个图像库网站,我有一系列按钮,如approve Image和download Image,它们调用JS函数“runAJAX”,根据请求类型,php应该预先准备一个特定的任务。我遇到的问题是,AJAX函数表示它已成功运行,但php文件没有运行。当我运行它时,“Request Completed”会打印出来,但不会像预期的那样创建txt文件。任何帮助都将受到极大的感谢。多谢各位Javascript Ajax函数不调用php脚本,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,因此,我正在创建一个图像库网站,我有一系列按钮,如approve Image和download Image,它们调用JS函数“runAJAX”,根据请求类型,php应该预先准备一个特定的任务。我遇到的问题是,AJAX函数表示它已成功运行,但php文件没有运行。当我运行它时,“Request Completed”会打印出来,但不会像预期的那样创建txt文件。任何帮助都将受到极大的感谢。多谢各位 // imageID is an int of the image UID // request is
// imageID is an int of the image UID
// request is a string that describes the button (approve, download etc.)
function runAJAX(imageID, request) {
// myArr is a JSON array
var myArr = allJSON;
console.log("UID is" + imageID);
for (var i = 0; i < myArr.length; i++){
if (myArr[i].UID == imageID){
switch (request) {
case "approve": myArr[i].approved = "true"; console.log("Approve was set to true"); break;
case "download": console.log("Image will be downloaded"); break;
default: console.log("No action will be done for this request");
} // switch
} // if
} // for
$.ajax({
type: "POST",
url: 'requestAJAX.php',
data: {request: request, newJSON: JSON.stringify(myArr), download: imageID},
success: function(){
console.log("Request Completed: " + request);
window.location.reload(false);
} // success
});
} // runAJAX
//imageID是图像UID的int
//请求是描述按钮(批准、下载等)的字符串
函数runAJAX(imageID,请求){
//myArr是一个JSON数组
var myArr=allJSON;
日志(“UID是”+imageID);
对于(变量i=0;i
requestAJAX.php
<?php
switch ($_POST["request"]) {
case "approve":
// output to checking to see what the request is
touch("AJAXtest.txt");
file_put_contents("AJAXtest.txt", $_POST["request"]);
unlink("test.json");
$stringyJSON = json_decode($_POST["newJSON"], true);
$finalJSON = json_encode($stringyJSON, JSON_PRETTY_PRINT);
$file = "galleryinfo.json";
touch($file);
file_put_contents($file, $finalJSON);
break;
case "download":
// output for checking to see what the request is
touch("AJAXtest.txt");
file_put_contents("AJAXtest.txt", $_POST["request"]);
// Get parameters
$file = $_POST["download"];
$filepath = "UploadedImages/" . $file;
if(file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filepath).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filepath));
flush(); // Flush system output buffer
readfile($filepath);
exit;
} // if
break:
default:
// output for checking to see what the request is
touch("AJAXtest.txt");
file_put_contents("AJAXtest.txt", "No request made");
echo "No request was made";
}
?>
不创建文件与根本不运行的PHP脚本不同。你的第一项工作就是找出它到底是哪一个。您是否尝试过使用浏览器的开发工具调试AJAX请求?您是否在PHP端打开了错误日志记录?另一方面,通过AJAX下载文件将不起作用。数据只会在JavaScript代码中的一个变量中结束。它不会像常规HTTP请求那样生成文件下载。你确定你在函数request
parameterrunAJAX(imageID,request)
@ADyson中传递了正确的值吗?谢谢你的回答:)我没有使用任何开发工具,但这肯定是我要研究的。另外,我下载的是php而不是ajax,但感谢您让我知道,如果我在下载过程中遇到任何问题,这肯定会有所帮助。@RiggsFolly我做了大量的测试并打印了这些值,是的,它们总是应该是的。。。