DmitrMakeev commited on
Commit
82fc4ed
·
verified ·
1 Parent(s): 0bf0455

Update nutri_call.html

Browse files
Files changed (1) hide show
  1. nutri_call.html +21 -53
nutri_call.html CHANGED
@@ -326,13 +326,9 @@
326
 
327
  <script>
328
  document.getElementById('calculate-btn').addEventListener('click', function() {
329
- // 1. Собираем данные с защитой от null/undefined
330
- const getValue = (id) => {
331
- const val = parseFloat(document.getElementById(id)?.value);
332
- return isNaN(val) ? 0 : val;
333
- };
334
 
335
- // 2. Формируем запрос
336
  const requestData = {
337
  fertilizerConstants: {
338
  "Кальциевая селитра": {
@@ -372,70 +368,42 @@ document.getElementById('calculate-btn').addEventListener('click', function() {
372
  }
373
  };
374
 
375
- console.log("Отправляемые данные:", JSON.stringify(requestData, null, 2));
376
 
377
- // 3. Отправка на сервер
378
  fetch('/calculation', {
379
  method: 'POST',
380
- headers: {
381
- 'Content-Type': 'application/json',
382
- },
383
  body: JSON.stringify(requestData)
384
  })
385
- .then(response => {
386
- if (!response.ok) {
387
- return response.json().then(err => {
388
- throw new Error(err.message || `HTTP error! status: ${response.status}`);
389
- });
390
- }
391
- return response.json();
392
- })
393
  .then(data => {
394
  console.log("Ответ сервера:", data);
 
395
 
396
- // 4. Обработка ответа
397
- if (data.error) {
398
- throw new Error(data.error);
399
- }
400
-
401
- // Заполнение результатов
402
- const fertilizerMap = {
403
- "Сульфат магния": "magnesium_sulfate",
404
  "Кальциевая селитра": "calcium_nitrate",
405
- "Монофосфат калия": "monopotassium_phosphate",
406
  "Аммоний азотнокислый": "ammonium_nitrate",
407
- "Калий сернокислый": "potassium_sulfate",
408
- "Калий азотнокислый": "potassium_nitrate"
 
409
  };
410
 
411
- // Сначала обнуляем все поля
412
- Object.values(fertilizerMap).forEach(id => {
413
- const field = document.getElementById(id);
414
- if (field) field.value = "0";
415
  });
416
 
417
- // Заполняем данные из ответа
418
- if (data.fertilizers && Array.isArray(data.fertilizers)) {
419
- data.fertilizers.forEach(fert => {
420
- const fieldId = fertilizerMap[fert.name];
421
- if (fieldId) {
422
- const field = document.getElementById(fieldId);
423
- if (field) field.value = fert.grams?.toFixed(3) || "0";
424
- }
425
- });
426
- }
427
-
428
- // Выводим EC
429
- if (data.ec !== undefined) {
430
- const ecField = document.getElementById('ec-value');
431
- if (ecField) ecField.textContent = data.ec.toFixed(2);
432
- }
433
-
434
- // alert("Расчет успешно завершен!");
435
  })
436
  .catch(error => {
437
  console.error("Ошибка:", error);
438
- alert(`Ошибка расчета: ${error.message}`);
439
  });
440
  });
441
  </script>
 
326
 
327
  <script>
328
  document.getElementById('calculate-btn').addEventListener('click', function() {
329
+ const getValue = (id) => parseFloat(document.getElementById(id)?.value) || 0;
 
 
 
 
330
 
331
+ // Формируем запрос в правильном формате
332
  const requestData = {
333
  fertilizerConstants: {
334
  "Кальциевая селитра": {
 
368
  }
369
  };
370
 
371
+ console.log("Отправка данных:", JSON.stringify(requestData, null, 2));
372
 
 
373
  fetch('/calculation', {
374
  method: 'POST',
375
+ headers: { 'Content-Type': 'application/json' },
 
 
376
  body: JSON.stringify(requestData)
377
  })
378
+ .then(response => response.json())
 
 
 
 
 
 
 
379
  .then(data => {
380
  console.log("Ответ сервера:", data);
381
+ if (data.error) throw new Error(data.error);
382
 
383
+ // Заполняем результаты
384
+ const fertilizers = {
 
 
 
 
 
 
385
  "Кальциевая селитра": "calcium_nitrate",
386
+ "Калий азотнокислый": "potassium_nitrate",
387
  "Аммоний азотнокислый": "ammonium_nitrate",
388
+ "Сульфат магния": "magnesium_sulfate",
389
+ "Монофосфат калия": "monopotassium_phosphate",
390
+ "Калий сернокислый": "potassium_sulfate"
391
  };
392
 
393
+ Object.values(fertilizers).forEach(id => {
394
+ document.getElementById(id).value = "0";
 
 
395
  });
396
 
397
+ data.fertilizers?.forEach(fert => {
398
+ const fieldId = fertilizers[fert.name];
399
+ if (fieldId) {
400
+ document.getElementById(fieldId).value = fert.grams?.toFixed(3) || "0";
401
+ }
402
+ });
 
 
 
 
 
 
 
 
 
 
 
 
403
  })
404
  .catch(error => {
405
  console.error("Ошибка:", error);
406
+ alert(`Ошибка: ${error.message}`);
407
  });
408
  });
409
  </script>