Answer the question
In order to leave comments, you need to log in
Correct authorization?
Hello. I ask for help. I make a request for authorization and receiving a token to the server https://fan-foto-ml753mfuuq-uc.a.run.app/ (login method). I make a request through Fetch.
Initially got ' Access to fetch at ' https://fan-foto-ml753mfuuq-uc.a.run.app/api/v1/lo... ' from origin ' localhost:3000 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource .
Installed a CORS app for Chrome and now I'm getting " Unhandled Rejection (Error): Not Fetch /api/v1/login/access-token , recived 405 "
_apiBase = 'https://fan-foto-ml753mfuuq-uc.a.run.app';
proxyurl = "https://cors-anywhere.herokuapp.com/";
getResource = async (url) => {
const res = await fetch(`${this._apiBase}${url}`);
if (!res.ok) {
throw new Error(`Not Fetch ${url} , recived ${res.status}`)
}
const body = await res.json();
return body;
};
getToken = async (username, password) => {
let user = {
username,
password
};
const token = await this.getResource(`/api/v1/login/access-token`, {
// method: 'POST',
method: 'OPTIONS',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer TOKEN',
// 'Access-Control-Allow-Origin': 'http://127.0.0.1:3000',
// 'Access-Control-Allow-Methods': 'GET, POST, HEAD, OPTIONS',
// 'Access-Control-Allow-Headers': 'Content-Type, Authorization',
// 'Access-Control-Allow-Credentials': true
},
// credentials: true,
// origin: 'http://127.0.0.1:3000',
// accept: 'application/json',
body: JSON.stringify(user)
}
);
return token;
};
Answer the question
In order to leave comments, you need to log in
so you take getResource = async (url)
one argument, and you pass two , and the second one, of course, does not pass
this.getResource(`/api/v1/login/access-token`, {
405 error - because you are using the OPTIONS method, but you most likely need POST.
Try to specify one of the settings in headers: cors, no-cors, same-origin or navigate.
https://developer.mozilla.org/en/docs/Web/API/Requ...
We work locally - turn on .
We stop working - turn it off (some sites stop working with this plugin).
On the server, the error related to CORS will disappear.
At least that's how it was for me.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question