Fetch
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
(async () => { const rawResponse = await fetch('https://b24.your-site.ru/local/api/dealBatchChecker.php', { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ deal_id:deal_id, batch_date:batch_date, batch_number:batch_number, } ) }); const res = await rawResponse.json(); console.log(res); if(res.ids.length > 0) { var reclamationsIds = res.ids; function createLink(id) { var baseUrl = "https://b24.your-site.ru/crm/deal/details/"; return '<a href="' + baseUrl + id + '/">' + id + '</a>'; } var linksArray = reclamationsIds.map(createLink); var resultString = 'ID рекламаций ' + linksArray.join(', '); console.log(resultString); var popup = new BX.CDialog({ 'title': 'По данной партии уже были рекламации!', 'content': resultString, 'draggable': true, 'resizable': true, 'buttons': [BX.CDialog.btnClose] }); popup.Show(); } })(); |
Ajax
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// Создаем новый экземпляр XMLHttpRequest для выполнения AJAX-запроса var xhr = new XMLHttpRequest(); // Устанавливаем URL и метод запроса var url = 'https://b24.your-site.ru/local/api/dealBatchChecker.php'; var method = 'POST'; // Формируем объект с данными для отправки var dataToSend = { deal_id: deal_id, batch_date: batch_date, batch_number: batch_number }; // Преобразуем объект в JSON var jsonData = JSON.stringify(dataToSend); // Устанавливаем заголовки запроса xhr.open(method, url, true); xhr.setRequestHeader('Accept', 'application/json'); xhr.setRequestHeader('Content-Type', 'application/json'); // Обработчик ответа от сервера xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status === 200) { var res = JSON.parse(xhr.responseText); console.log(res); if (res.ids.length > 0) { var reclamationsIds = res.ids; function createLink(id) { var baseUrl = "https://b24.your-site.ru/crm/deal/details/"; return '<a href="' + baseUrl + id + '/">' + id + '</a>'; } var linksArray = reclamationsIds.map(createLink); var resultString = 'ID рекламаций ' + linksArray.join(', '); console.log(resultString); var popup = new BX.CDialog({ 'title': 'По данной партии уже были рекламации!', 'content': resultString, 'draggable': true, 'resizable': true, 'buttons': [BX.CDialog.btnClose] }); popup.Show(); } } else { console.error('Ошибка при выполнении запроса:', xhr.status); } } }; // Отправляем JSON данные на сервер xhr.send(jsonData); |
Jquery
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
var $form = $(form); var data = $form.serialize(); $form.addClass('load'); $.ajax({ url: 'https://b24.your-site.ru/local/webhooks/hb_calendar_add.php', type: 'POST', data: data, dataType: 'html', success: function (res) { console.log(res); const userId = "<?=$userId?>"; try { $form.removeClass('load'); } catch(e){ console.log(e); } Swal.fire({ icon: 'success', title: 'Форма была успешно отправлена!', showConfirmButton: true, willClose: () => { window.location.href = `/company/personal/user/${userId}/calendar/`; } }); }, error: function (res) { // <-- Добавить запятую после блока "error" console.log(res); } }); |
Обработчик на PHP для jquery
|
1 2 3 4 5 6 7 8 9 |
$name = $_POST['name']; $user_id = $_POST['user_id']; $data = [ "name" => $name, "user_id" => $user_id, ]; echo json_encode($data); |
Обработчик на PHP для JS и Fetch для приема json из приведенных выше примеров
|
1 2 3 4 5 6 7 8 9 10 11 |
$payload = json_decode(@\file_get_contents('php://input'), true); //file_put_contents($_SERVER['DOCUMENT_ROOT'].'/log-test12.txt', print_r($payload, true), FILE_APPEND); if (isset($payload['deal_id'])) { $deal_id = $payload['deal_id']; } echo json_encode($deal_id); |
