Ajax请求什么都不返回。为什么?

时间:2014-06-19 06:43:03

标签: php 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文件中有问题。请帮忙。

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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');

答案 2 :(得分:1)

执行jquery + 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();
    }
?>