Почему передается неверный url?

Рейтинг: 0Ответов: 1Опубликовано: 17.06.2023

Запрос выдает ошибку SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON введите сюда описание изображения

В response url приходит адрес 'http://localhost:3000/people/1/https://swapi.dev/api/' а должен https://swapi.dev/api/people/1/

export default function Form(props) {
    const classes = useStyles(props);
    const inputRef = useRef(null);
    const formRef = useRef(null);
    const onSubmit = (event) => {
        event.preventDefault();
        const url = `${inputRef.current.value}${formRef.current.action}/`
        console.log(inputRef.current.value)
        console.log(formRef.current.action)
        fetch(url)
            .then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                console.log('response: ', response);
                return response.json();
            })
            .then(data => {
                console.log(data)
            })
            .catch(error => {
                console.error('Error: ',error)
            })

    }
    return (
        <form
            className={`${classes["swapi__form"]}`}
            action="https://swapi.dev/api"
            method="get"
            onSubmit={onSubmit}
            ref={formRef}
        >

            <span className={`${classes["swapi__url-span"]}`}>https://swapi.dev/api</span>

            <label className={`${classes["swapi__label"]}`}>
                <input
                    className={`${classes["swapi__input"]}`}
                    type="text"
                    placeholder="/people/1/"
                    ref={inputRef}
                />
            </label>

            <button className={`${classes["swapi__button"]}`}>Get info</button>
        </form>
    )
}

Ответы

▲ 1Принят

У тебя в описании вопроса адрес https://swapi.dev/api/people/1/ а на скрине к адресу https://swapi.dev/api/people/1/ прилеплен еще https://swapi.dev/api. Возможно в Url приходит кривой адрес.