我在頂部有一個按鈕,顯示一個模式窗口來啓動一個VOIP呼叫 - 最終的目的是讓它呼叫列表中的第一個數字,然後是第二個號碼等。我有它的工作原理,它顯示模式窗口,並允許我發起呼叫列表中的第一個號碼。AJAX請求 - 添加額外的GET請求內部循環
我現在需要更新腳本,以便如果調用成功,它會在循環中發出另一個AJAX請求,每5秒檢查一次調用的狀態。如果第一次調用成功,將返回的我存儲在httpResponseText
變量如下:
Authentication accepted<br/>ActionID = Jo9oACY52cp1
我需要解析出ActionID
值 - 在上面的例子中,這將是Jo9oACY52cp1
- 然後有另一GET請求通過傳遞ActionID來得到當前呼叫的狀態如下:
https://www.acme.com/GetStatus.php?ActionID=$action_id
該請求將返回3個值 - ActionID,UID,狀態 - 就像這樣:
xshsJ6Y2eLDC,1500806656.160,ANSWER
我只對第三個值 - STATUS感興趣 - 我需要不斷檢查此請求的結果,直到狀態不等於IN_PROGRESS
。此時我可以啓用Next Call按鈕並重新開始。
這是我當前的表和腳本:
$("#startBulkContactCall").click(function() {
$(this).attr('selectedRow', '1');
contactMobile = $($($('table > tbody > tr:nth-child(1) > td:nth-child(2)').children())[0]).attr('contactMobile');
contactName = $($($('table > tbody > tr:nth-child(1) > td:nth-child(2)').children())[0]).attr('contactName');
$('#callNextBulkContact').prop('disabled', true);
firstURL = "<?php echo $callBackURL ;?>" + defaultCallBackNumber + "<?php echo $contactCallBack ;?>" + contactMobile;
console.log('firstURL: ' + firstURL);
$.ajax({
url: "<?php echo $callBackURL ;?>" + defaultCallBackNumber + "<?php echo $contactCallBack ;?>" + contactMobile,
data: {},
type: "GET"
})
.then(function(data, status, xhr) {
var httpStatus = status;
var httpResponseCode = (xhr.status);
var httpResponseText = (xhr.responseText);
$('#ajaxSuccessBulk').html('Call in Progress').show();
$("#startBulkContactCall").prop("disabled", true);
$("#callNextBulkContact").prop("disabled", true);
})
.fail(function(xhr) {
var httpStatus = (xhr.status);
var httpResponseCode = (xhr.getAllResponseHeaders);
var httpResponseText = (xhr.responseText);
var ajaxError = 'There was an error requesting the call back. HTTP Status: ' + httpStatus + ' ' + httpResponseText;
//make alert visible
$('#ajaxError').html(ajaxError).show();
})
});
$('#callNextBulkContact').click(function() {
$('#callBulkContact').attr('selectedRow', parseInt($('#callBulkContact').attr('selectedRow')) + 1);
var rowNum = parseInt($('#callBulkContact').attr('selectedRow'));
var row = 'table > tbody > tr:nth-child(' + rowNum + ') > td:nth-child(2)';
contactMobile = $($($(row).children())[0]).attr('contactMobile');
contactName = $($($(row).children())[0]).attr('contactName');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<button type="button" id="bulkCallButton" class="btn btn-default"><span class="glyphicon glyphicon-phone"></span> Bulk Call</button>
<table class="table table2 table-striped table-bordered" width="100%">
<thead>
<th scope="col">Name</th>
<th scope="col">Mobile</th>
</thead>
<tbody>
<tr id="D8F49748-212A-42D8-A188-4C23556027FA">
<td><a href="details.php?action=contactLink&contactID=D8F49748-212A-42D8-A188-4C23556027FA">John Citizen</a></td>
<td><a href="#" contactName="John Citizen" contactMobile="0412345678" data-toggle="modal" data-rec-id="1537" data-target="#contactCallModal"><span class="glyphicon glyphicon-earphone"></span> 0412 345 678</a></td>
</tr>
<tr id="EAD2DCCA-7EFA-B048-AD7D-8FCC0ED5EFD7">
<td><a href="details.php?action=contactLink&contactID=EAD2DCCA-7EFA-B048-AD7D-8FCC0ED5EFD7">Jonah McMahon</a></td>
<td><a href="#" contactName="Jonah McMahon" contactMobile="0490876543" data-toggle="modal" data-rec-id="1538" data-target="#contactCallModal"><span class="glyphicon glyphicon-earphone"></span> 0490 876 543</a></td>
</tr>
<tr id="D9AA7744-E138-4A0E-86A2-B8D0CD2007D6">
<td><a href="details.php?action=contactLink&contactID=D9AA7744-E138-4A0E-86A2-B8D0CD2007D6">Jake Simpson</a></td>
<td><a href="#" contactName="Jake Simpson" contactMobile="0405999666" data-toggle="modal" data-rec-id="1577" data-target="#contactCallModal"><span class="glyphicon glyphicon-earphone"></span> 0405 999 666</a></td>
</tr>
</tbody>
</table>
<div class="modal" id="contactBulkCallModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Call Contact</h4>
</div>
<div class="modal-body">
<p>Calling </p>
</div>
<div id="ajaxError" class="alert alert-danger text-center" role="alert" style="display:none">
Error Response
</div>
<div id="ajaxSuccess" class="alert alert-success text-center" role="alert" style="display:none">
Call in Progress
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="startBulkContactCall" class="btn btn-success">Start Call</button>
<button type="button" id="callNextBulkContact" class="btn btn-success">Next</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
從來沒有做這樣一個複雜的Ajax請求(至少對我來說),而不是知道從哪裏開始添加額外的要求循環?
如何獲得電話號碼? – Salketer
電話號碼從數據庫查詢中返回,然後顯示在HTML表格中。 – user982124