DmitrMakeev commited on
Commit
24e7588
·
verified ·
1 Parent(s): d5bbfd5

Update nutri_call.html

Browse files
Files changed (1) hide show
  1. nutri_call.html +78 -44
nutri_call.html CHANGED
@@ -326,75 +326,109 @@
326
 
327
  <script>
328
  document.getElementById('calculate-btn').addEventListener('click', function() {
329
- const getValue = (id) => parseFloat(document.getElementById(id)?.value) || 0;
 
 
 
 
 
 
 
 
 
330
 
 
331
  const requestData = {
332
  fertilizerConstants: {
333
- "CaN2O6": {
334
- "NO3": getValue('fert_ca_no3') / 100,
335
  "Ca": getValue('fert_ca_ca') / 100
336
  },
337
- "KNO3": {
338
- "NO3": getValue('fert_kno3_no3') / 100,
339
  "K": getValue('fert_kno3_k') / 100
340
  },
341
- "NH4NO3": {
342
- "NO3": getValue('fert_nh4no3_no3') / 100,
343
- "NH4": getValue('fert_nh4no3_nh4') / 100
344
  },
345
- "MgSO4": {
346
  "Mg": getValue('fert_mgso4_mg') / 100,
347
  "S": getValue('fert_mgso4_s') / 100
348
  },
349
- "KH2PO4": {
350
  "P": getValue('fert_kh2po4_p') / 100,
351
  "K": getValue('fert_kh2po4_k') / 100
352
  },
353
- "K2SO4": {
354
  "K": getValue('fert_k2so4_k') / 100,
355
  "S": getValue('fert_k2so4_s') / 100
356
  }
357
  },
358
  profileSettings: {
359
- 'P': getValue('profile_p'),
360
- 'K': getValue('profile_k'),
361
- 'Mg': getValue('profile_mg'),
362
- 'Ca': getValue('profile_ca'),
363
- 'S': getValue('profile_s'),
364
- 'NO3': getValue('profile_no3'),
365
- 'NH4': getValue('profile_nh4'),
366
- 'liters': parseInt(document.getElementById('liters-input')?.value) || 1
367
  }
368
  };
369
 
370
- fetch('/calculation', {
371
- method: 'POST',
372
- headers: { 'Content-Type': 'application/json' },
373
- body: JSON.stringify(requestData)
374
- })
375
- .then(response => response.json())
376
- .then(data => {
377
- const fertilizers = {
378
- "CaN2O6": "calcium_nitrate",
379
- "KNO3": "potassium_nitrate",
380
- "NH4NO3": "ammonium_nitrate",
381
- "MgSO4": "magnesium_sulfate",
382
- "KH2PO4": "monopotassium_phosphate",
383
- "K2SO4": "potassium_sulfate"
384
- };
 
 
 
 
 
 
 
 
385
 
386
- Object.values(fertilizers).forEach(id => {
387
- document.getElementById(id).value = "0";
388
- });
 
389
 
390
- data.fertilizers?.forEach(fert => {
391
- const fieldId = fertilizers[fert.name];
392
- if (fieldId) {
393
- document.getElementById(fieldId).value = fert.grams?.toFixed(3) || "0";
 
 
 
 
 
 
 
 
 
 
394
  }
395
- });
396
- })
397
- .catch(error => alert("Ошибка: " + error));
 
 
 
 
 
398
  });
399
  </script>
400
 
 
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