DmitrMakeev commited on
Commit
8436df1
·
verified ·
1 Parent(s): 467aacd

Update nutri_call.html

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