2014-06-19 128 views
1

以下是ajax請求。Ajax請求不返回任何內容。爲什麼?

$.post('delete.php', {'deletearray':deletearray, 'dir':dir}, function(deleted, undeleted){ 
    if(undeleted == 0) { 
     alert('All ' + deleted + ' files delted from the server'); 
    } else { 
     alert(deleted + ' files deleted and ' + undeleted + ' files could not be deleted'); 
    } 
}, 'json'); 

,並在這裏不用的delete.php

<?php 
    if(isset($_POST['deletearray'])) { 
     $files = $_POST['deletearray']; 
     $dir = $_POST['dir']; 
     $deleted = 0; 
     $undeleted = 0; 

     foreach($files as $file) { 
      if(unlink($dir.$file) && unlink($dir.'thumb/'.$file)) { 
       $deleted ++; 
      } else { 
       $undeleted ++; 
      } 
     } 
     echo json_encode($deleted, $undeleted); 
    } 
    return; 
?> 

截至上運行它成功地刪除文件,但沒有消息顯示的代碼。

我還試圖改變AJAX請求爲:

$.post('delete.php', {deletearray:deletearray, dir:dir}, function(deleted, undeleted){ 
    alert("php finished"); 
}, 'json'); 

仍然不顯示該消息。所以我想在delete.php文件中有錯誤。請幫忙。

+0

如果問題是在PHP中,你爲什麼不檢查錯誤日誌?或者JavaScript控制檯呢! –

+5

您可能正在尋找'$ _POST ['deletearray']',而不是'$ _POST [deletearray]'。 – sevenseacat

+1

也是'$ _POST ['dir']' – UserProg

回答

1

做jquer的最好方法Y + AJAX + PHP是爲下一個:

的jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script type="text/javascript"> 

function do_ajax() { 
     //set data 
     var myData = new Array(); 
     myData.push({name:'deletearray',value:'deletearray'}); 
     myData.push({name:'dir',value:'dir'}); 
     //ajax post 
     $.ajax({ 
      dataType: 'json', 
      url: 'delete.php', 
      type: 'post', 
      data: myData, 
      success: function(returnData) { 
       if(returnData.undeleted == 0) { 
        alert('All ' + returnData.deleted + ' files delted from the server'); 
       } else { 
        alert(returnData.deleted + ' files deleted and ' + returnData.undeleted + ' files could not be deleted'); 
       } 
      } 
     }); 
}    
</script> 

PHP:

<?php 
    $myData = $_POST; 
    if(isset($myData['deletearray']) AND isset($myData['dir'])) { 
     $files = $myData['deletearray']; 
     $dir = $myData['dir']; 
     $deleted = 0; 
     $undeleted = 0; 

     foreach($files as $file) { 
      if(unlink($dir.$file) && unlink($dir.'thumb/'.$file)) { 
       $deleted ++; 
      } else { 
       $undeleted ++; 
      } 
     } 
     print(json_encode(array('deleted' => $deleted, 'undeleted' => $undeleted))); 
     exit(); 
    } 
?> 
1

您應該使用json_encode類似以下內容:

json_encode(array('deleted' => $deleted, 'undeleted' => $undeleted)); 

而且你必須讓增值經銷商與data.undeleteddata.deleted

$.post('delete.php', {'deletearray':deletearray, 'dir':dir}, function(data) { 
    if(data.undeleted == 0) { 
     alert('All ' + data.deleted + ' files delted from the server'); 
    } else { 
     alert(data.deleted + ' files deleted and ' + data.undeleted + ' files could not be deleted'); 
    } 
}, 'json'); 
+0

echo json_encode($ filenameArray)效果很好... – Tom

+0

你的方法會起作用,應該是數組。 'json_encode($ deleted,$ undeleted)'是錯的 – Bora

+0

你說得對。只有數組返回與json。我的問題仍然存在。 – Tom

2

首先東西─

使用$_POST['deletearray'],而不是$_POST[deletearray]

其次東西─

你不能從PHP scrtipt返回不同的變量,每次打印有在AJAX回調返回的事,所以只寫這個 -

PHP

json_encode(array('totalDeleted' => $deleted, 'totalUndeleted' => $undeleted)); 

AJAX

... 
function(response){ 
    response=JSON.parse(response); 
    console.log(response); 
} 
相關問題