SyntaxError: Beklenmeyen belirteç

0

Soru

Form verilerini göndermeye ve API'DE bir post isteği yapmaya ve tahminler almaya çalışıyorum ancak hatayı alıyorum SyntaxError: Unexpected token < in JSON at position 0 yaparken. API'yi kontrol ettim ve gayet iyi. Aşağıdaki gönderi isteğini ekledim. Formlarla ilgili bir sorun olduğunu tahmin ediyordum. yanılıyorsam lütfen beni düzeltin.

enter image description here

const FirstProject = () => {
  const [solutestate, setSoluteState] = useState("");
  const [solventstate, setSolventState] = useState("");
  const [fetchData, setFetchData] = useState("");
  const [Error, setError] = useState(null);

  const { register, handleSubmit, control } = useForm({
    defaultValues: {
      solute: "",
      solvent: "",
    },
  });

  const formData = new FormData();
  const onSubmit = (data) => {
    formData.set("solute", data.solute);
    formData.set("solvent", data.solvent);

    fetch("https://flask-api-test1.herokuapp.com/predict", {
      method: "post",
      body: formData,
    })
      .then((res) => res.json())
      .then((result) => {
        setFetchData(result.result.predictions);
        //console.log(result.result.predictions);
        //console.log(Object.entries(result));
        // setIsPending(false);
      })
      .catch((err) => {
        console.log(data);
        setError(err.error);
        console.log(err);
      });
  };

<form onSubmit={handleSubmit(onSubmit)}>
 <input
  {...register("solute")}
  placeholder="First Name"
  onChange={(e) => setSoluteState(e.target.value)}
  value={solutestate}
 />
 <input
  {...register("solvent")}
  placeholder="First Name"
  onChange={(e) => setSolventState(e.target.value)}
  value={solventstate}
 />
 <input type="submit" />
 </form>

enter image description here enter image description here

fetch fetch-api forms react-hook-form
2021-11-22 15:14:44
1

En iyi cevabı

0

Bu json() yöntemi Response arayüz bir Yanıt akışı alır ve tamamlanana kadar okur. Gövde metnini aşağıdaki gibi ayrıştırmanın sonucu olarak çözen bir söz verir JSON ama isteğiniz API geri dönüş 500 durum kodu. Bu şekilde halledebilirsin :

.then((res) => {
    if (res.ok) {
       return res.json();
    }
    throw "Something went wrong";
 })

yerine

.then((res) => res.json())
2021-11-22 15:25:33

Ben Başlarken "Bir şeyler yanlış gitti" ne zaman koştu ben senin kodu
harsh

Tabii ki, bu kod çalışır throw "Something went wrong"; }), Sunucu API'si bozuldu ve isteğiniz geri döndü 500 Internal Server Error.
Vitaliy Rayets

@ harsh apı'ye bastığınızda sunucunuz çöküyor. sunucunuzda neyin yanlış olduğunu hata ayıklamanız gerekir.
abhi patil

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................