When i register to my form it’s not work because :
Access to fetch at ‘http://127.0.0.1:8081/api/auth/register’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
i use react js for my front end :
export default function Inscrit() {
function Alerte(e, props) {
e.preventDefault()
fetch('http://127.0.0.1:8081/api/auth/register', {
method: 'POST',
mode: 'cors',
credentials: "include",
headers: {'Content-Type':'application/json'}})
}
return(
<div>
<h1>Inscrit toi !</h1>
<form>
<input type="text" placeholder="Nom" name='nom'/>
<input type="text" placeholder="Prenom" name='prenom'/>
<input type="email" placeholder="Email" name='email'/>
<input type="password" placeholder="Mot de passe" name='password'/>
<button type="submit" onClick={Alerte}>s'inscrire</button>
</form>
</div>
)
}
and my back end in java eclipse also my Cors:
spring:
cloud:
gateway:
globalcors:
corsConfigurations:
'[/**]':
allowedOrigins: "http://127.0.0.1:3000"
allowedHeaders: "*"
allowedMethods:
- GET
- POST
- PUT
- DELETE
2
Answers
In your java backend config file, add OPTIONS to the allowedMethods. OPTIONS requests are often used in conjunction with CORS requests to determine whether a cross-origin request is allowed.
in your app.js or any of your request in front use:
or
and