試圖從ajax獲取json數組,但是當我試圖在文本文件中寫下它時,它什麼也沒有顯示。PHP沒有得到AJAX JSON數據
var img = JSON.parse(localStorage.getItem("iPath"));
var img = JSON.stringify(img);
console.log(img);
$.ajax({
url: './php/temporary.php?deletefile',
cache: false,
type: 'POST',
data: img,
success: function(respond, textStatus, jqXHR){
if(typeof respond.error === 'undefined'){
//window.location.assign("/buyplace.html");
}
else{
console.log('ОШИБКИ ОТВЕТА сервера: ' + respond.error);
}
},
error: function(jqXHR, textStatus, errorThrown){
console.log('ОШИБКИ AJAX запроса: ' + textStatus);
}
});
if(isset($_GET['deletefile'])){
$params = json_decode($_POST);
$myfile = fopen("testfile.txt", "w");
fwrite($myfile, $params);
//$img = "uploads/" . $imgPath;
//move_uploaded_file($imgPath, "./uploads/");
//unlink('./uploads/' . $img);
}
?>
我該如何解決這個問題?
在你的AJAX調用是POST的類型和PHP正在尋找它的GET php的改變 如果(isset($ _ POST [ '' DELETEFILE])){}。您的ajax調用中的deletefile將變爲空且未設置也嘗試更改.php?deletefile = true – dsadnick
您可能需要使用'$ jsondata = json_decode(file_get_contents('php:// input'))' – Scuzzy
... $ _GET ['deletefile']'在URL行上,所以它仍然應該被填充。 – Scuzzy