DmitrMakeev commited on
Commit
f185dde
·
verified ·
1 Parent(s): 38c91f0

Update nutri_call.html

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