DmitrMakeev commited on
Commit
57db443
·
verified ·
1 Parent(s): ddf83cd

Update nutri_call.html

Browse files
Files changed (1) hide show
  1. nutri_call.html +82 -36
nutri_call.html CHANGED
@@ -325,64 +325,110 @@
325
 
326
 
327
  <script>
328
-
329
  document.getElementById('calculate-btn').addEventListener('click', function() {
330
- // 1. Собираем данные с формы
 
 
 
 
 
 
 
 
 
 
 
331
  const requestData = {
332
  fertilizerConstants: {
333
  "Кальциевая селитра": {
334
- "N (NO3-)": parseFloat(document.getElementById('fert_ca_no3').value) / 100,
335
- "Ca": parseFloat(document.getElementById('fert_ca_ca').value) / 100
336
  },
337
  "Калий азотнокислый": {
338
- "N (NO3-)": parseFloat(document.getElementById('fert_kno3_no3').value) / 100,
339
- "K": parseFloat(document.getElementById('fert_kno3_k').value) / 100
340
  },
341
  "Аммоний азотнокислый": {
342
- "N (NO3-)": parseFloat(document.getElementById('fert_nh4no3_no3').value) / 100,
343
- "N (NH4+)": parseFloat(document.getElementById('fert_nh4no3_nh4').value) / 100
344
  },
345
  "Сульфат магния": {
346
- "Mg": parseFloat(document.getElementById('fert_mgso4_mg').value) / 100,
347
- "S": parseFloat(document.getElementById('fert_mgso4_s').value) / 100
348
  },
349
  "Монофосфат калия": {
350
- "P": parseFloat(document.getElementById('fert_kh2po4_p').value) / 100,
351
- "K": parseFloat(document.getElementById('fert_kh2po4_k').value) / 100
352
  },
353
  "Калий сернокислый": {
354
- "K": parseFloat(document.getElementById('fert_k2so4_k').value) / 100,
355
- "S": parseFloat(document.getElementById('fert_k2so4_s').value) / 100
356
  }
357
  },
358
  profileSettings: {
359
- 'P': parseFloat(document.getElementById('profile_p').value),
360
- 'K': parseFloat(document.getElementById('profile_k').value),
361
- 'Mg': parseFloat(document.getElementById('profile_mg').value),
362
- 'Ca': parseFloat(document.getElementById('profile_ca').value),
363
- 'S': parseFloat(document.getElementById('profile_s').value),
364
- 'N (NO3-)': parseFloat(document.getElementById('profile_no3').value),
365
- 'N (NH4+)': parseFloat(document.getElementById('profile_nh4').value),
366
  'liters': parseInt(document.getElementById('liters-input').value) || 1
367
  }
368
  };
369
 
370
- console.log("Отправляемые данные:", requestData);
 
 
371
 
372
- // 2. Отправка на сервер
373
- fetch('/calculation', {
374
- method: 'POST',
375
- headers: {
376
- 'Content-Type': 'application/json',
377
- },
378
- body: JSON.stringify(requestData)
379
- })
380
- .then(response => response.json())
381
- .then(data => {
382
- console.log("Ответ сервера:", data);
383
- // 3. Здесь будет обработка ответа и заполнение полей
384
- })
385
- .catch(error => console.error("Ошибка:", error));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
  });
387
  </script>
388
 
 
325
 
326
 
327
  <script>
 
328
  document.getElementById('calculate-btn').addEventListener('click', function() {
329
+ // 1. Функция для безопасного получения числового значения
330
+ const getValue = (id) => {
331
+ const element = document.getElementById(id);
332
+ const value = parseFloat(element.value);
333
+ if (isNaN(value)) {
334
+ console.error(`Ошибка: поле ${id} содержит не число! Значение:`, element.value);
335
+ return 0;
336
+ }
337
+ return value;
338
+ };
339
+
340
+ // 2. Формируем данные в ТОЧНОМ формате для сервера
341
  const requestData = {
342
  fertilizerConstants: {
343
  "Кальциевая селитра": {
344
+ "N (NO3-)": getValue('fert_ca_no3') / 100,
345
+ "Ca": getValue('fert_ca_ca') / 100
346
  },
347
  "Калий азотнокислый": {
348
+ "N (NO3-)": getValue('fert_kno3_no3') / 100,
349
+ "K": getValue('fert_kno3_k') / 100
350
  },
351
  "Аммоний азотнокислый": {
352
+ "N (NO3-)": getValue('fert_nh4no3_no3') / 100,
353
+ "N (NH4+)": getValue('fert_nh4no3_nh4') / 100
354
  },
355
  "Сульфат магния": {
356
+ "Mg": getValue('fert_mgso4_mg') / 100,
357
+ "S": getValue('fert_mgso4_s') / 100
358
  },
359
  "Монофосфат калия": {
360
+ "P": getValue('fert_kh2po4_p') / 100,
361
+ "K": getValue('fert_kh2po4_k') / 100
362
  },
363
  "Калий сернокислый": {
364
+ "K": getValue('fert_k2so4_k') / 100,
365
+ "S": getValue('fert_k2so4_s') / 100
366
  }
367
  },
368
  profileSettings: {
369
+ 'P': getValue('profile_k'),
370
+ 'K': getValue('profile_ca'),
371
+ 'Mg': getValue('profile_s'),
372
+ 'Ca': getValue('profile_mg'),
373
+ 'S': getValue('profile_cl'),
374
+ 'N (NO3-)': getValue('profile_no3'),
375
+ 'N (NH4+)': getValue('profile_nh4'),
376
  'liters': parseInt(document.getElementById('liters-input').value) || 1
377
  }
378
  };
379
 
380
+ // 3. Выводим данные для проверки
381
+ console.log("Данные для отправки на сервер:");
382
+ console.log(JSON.stringify(requestData, null, 2));
383
 
384
+ // 4. Проверка данных
385
+ let hasErrors = false;
386
+ const requiredFertilizers = ["Кальциевая селитра", "Калий азотнокислый", "Аммоний азотнокислый",
387
+ "Сульфат магния", "Монофосфат калия", "Калий сернокислый"];
388
+
389
+ for (const fert of requiredFertilizers) {
390
+ if (!requestData.fertilizerConstants[fert]) {
391
+ console.error(`Отсутствует удобрение: ${fert}`);
392
+ hasErrors = true;
393
+ }
394
+ }
395
+
396
+ const requiredElements = ['P', 'K', 'Mg', 'Ca', 'S', 'N (NO3-)', 'N (NH4+)'];
397
+ for (const elem of requiredElements) {
398
+ if (isNaN(requestData.profileSettings[elem])) {
399
+ console.error(`Некорректное значение для элемента ${elem}`);
400
+ hasErrors = true;
401
+ }
402
+ }
403
+
404
+ if (hasErrors) {
405
+ console.error("Обнаружены ошибки в данных! Отправка отменена.");
406
+ return;
407
+ }
408
+
409
+ // 5. Отправка данных на сервер
410
+ console.log("Отправка данных на сервер...");
411
+ const xhr = new XMLHttpRequest();
412
+ xhr.open("POST", "/calculation", true);
413
+ xhr.setRequestHeader("Content-Type", "application/json");
414
+
415
+ xhr.onreadystatechange = function() {
416
+ if (xhr.readyState === 4) {
417
+ console.log("Статус ответа:", xhr.status);
418
+ if (xhr.status === 200) {
419
+ console.log("Успешный ответ от сервера:", JSON.parse(xhr.responseText));
420
+ } else {
421
+ console.error("Ошибка сервера:", xhr.status, xhr.statusText);
422
+ console.error("Текст ошибки:", xhr.responseText);
423
+ }
424
+ }
425
+ };
426
+
427
+ xhr.onerror = function() {
428
+ console.error("Ошибка сети при отправке запроса");
429
+ };
430
+
431
+ xhr.send(JSON.stringify(requestData));
432
  });
433
  </script>
434