[英]jquery request running more than once
我有以下代碼向處理一些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請求失敗。 由於失敗,我認為它正在多次運行文件,因為我得到的結果超出了數據庫所需的結果。 如果我手動運行文件,則會得到所需的結果。 (旁注:盡管得到了錯誤,但仍運行processCalls.php的查詢)。 我希望這是有道理的。
謝謝!
檢查click函數是否多次綁定。
如果將click函數綁定多次,則該函數將被多次調用。
$("#applyCalls").off('click', callServer).on('click', callServer);
var callServer = function(){
$.get("admin/processCalls.php");
};
嘗試使用stopPropagation()
函數。
另外,由於您的PHP不返回任何內容,因此應使用POST
而不是GET
要了解POST
和GET
之間的區別,請查看本文
$("#applyCalls").click(function(event){
event.stopPropagation()
$.post("admin/processCalls.php");
});
你有嘗試過嗎?
$("#applyCalls").unbind("click").click(function(){$.get("admin/processCalls.php");});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.