Liza
Liza

Reputation: 11

JS, React How to pass JSON data with POST method to backend without OPTIONS request?

We're making a project with both frontend and backend, there is auth form, which should make POST request to backend, giving JSON object with email and password of user. If I specify 'Content-Type': 'application/json', then we see 2 requests instead of one: options (first) and post (second) requests, that results an error even with running backend (screenshot provides errors without running backend, only frontend) 2 req

const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: dataArr
            });

But when I specify 'Content-Type': 'application/x-www-form-urlencoded' and body: JSON.stringify(data), then there is no options request, only post one, but the data is then passed not as json object, so backend can't resolve the request properly. It awaits for json object post request result

const response = await fetch(url, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: JSON.stringify(data)
        });

Is there any way to pass data as JSON object via POST properly without OPTIONS requests? backend uses python FastAPI

Upvotes: 0

Views: 37

Answers (0)

Related Questions