jquery请求不止一次运行

时间:2014-04-16 07:48:24

标签: php jquery ajax request

我有这个代码来处理一个处理一些xls文件的文件(使用phpexcel库):

$("#applyCalls").click(function(){
    $.get("admin/processCalls.php");
});

processCalls.php如下所示:

include '../phpexcel2/Classes/PHPExcel.php';
include '../phpexcel2/Classes/PHPExcel/IOFactory.php';
date_default_timezone_set('Europe/Bucharest');

$fisierInbound = "inbound.xls";
$fisierOutbound = "outbound.xls";

callsInbound();

function callsInbound() {

        global $fisierInbound;
        global $current;

        global $con;

        $identify = PHPExcel_IOFactory::identify('daily/' . $fisierInbound);
        $objReader = PHPExcel_IOFactory::createReader($identify);
        $objReader->setReadDataOnly(true);
        $objPHPExcel = $objReader->load('daily/' . $fisierInbound);
        $worksheet = $objPHPExcel->setActiveSheetIndex(0);

        $query = mysqli_query($con, "SELECT * FROM users WHERE tip_user='agent' AND NOT (departament='online')");

        while($usr = mysqli_fetch_array($query)) {
                        $data[] = $usr;
        }

        $worksheetTitle     = $worksheet->getTitle();
        $highestRow         = $worksheet->getHighestRow(); // e.g. 10
        $highestColumn      = $worksheet->getHighestColumn(); // e.g 'F'
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
        $dataCalls = $worksheet->getCellByColumnAndRow(2, 2)->getValue();
        $dataSubstr = substr($dataCalls, 53);
        echo $dataSubstr;
        foreach ($data as $db) {

            for ($row = 6; $row <= $highestRow; ++ $row) {

                $marca      = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
                $nr         = $worksheet->getCellByColumnAndRow(4, $row)->getValue();

                if($db['marca'] == $marca) {
                    mysqli_query($con, "INSERT INTO dailycalls(data, nr, departament, oras, tip, id_user)
                                        VALUES('$dataSubstr', '$nr', '" . $db['departament'] . "', '" . $db['oras'] . "', 'inbound', '" . $db['id'] . "')");
                }
            }
        }
}

问题是我从processCalls得到“没有数据接收错误代码:ERR_EMPTY_RESPONSE”(chrome),因此ajax请求失败。由于它失败了,我认为它不止一次地运行该文件,因为我得到的结果比我在db中需要的更多。如果我手动运行文件,我会得到我需要的结果。 (旁注:尽管我得到了错误,但仍然会运行processCalls.php的查询)。我希望这是有道理的。

谢谢!

3 个答案:

答案 0 :(得分:0)

检查点击功能是否多次绑定。

如果点击功能多次绑定,则会多次调用该功能。

$("#applyCalls").off('click', callServer).on('click', callServer);

var callServer = function(){
    $.get("admin/processCalls.php");
};

答案 1 :(得分:0)

尝试使用stopPropagation()功能。

另外,由于您的PHP没有返回任何内容,您应该使用POST代替GET

要了解POSTGET之间的区别,请查看this article

$("#applyCalls").click(function(event){
    event.stopPropagation()
    $.post("admin/processCalls.php");
});

答案 2 :(得分:0)

你试过这个吗?

$("#applyCalls").unbind("click").click(function(){$.get("admin/processCalls.php");});