I try to send a post api request from a front end website (localhost:8888) to a golang backend (localhost:8000). I get the errors listed below. I looked on stackoverflow and the problems seems to be cross origin request and preflight request handling. I added the headers as shown below but the problem still occurs. I hope you guys can help me :)
Axios error:
OPTIONS http://localhost:8000/api/heimdall/signup 404 (Not Found)
Failed to load http://localhost:8000/api/heimdall/signup: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access. The response had HTTP status code 404.
Mux CORS handler
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})
originsOk := handlers.AllowedOrigins([]string{"*"})
headersOk := handlers.AllowedHeaders([]string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"})
log.Fatal(http.ListenAndServe(":8000", handlers.CORS(methodsOk, originsOk, headersOk)(r)))
Axois Code:
'use strict';
var axios = require('axios');
function Signup(email, password) {
var apiURL = 'http://localhost:8000/api/heimdall/signup';
return new Promise((resolve, reject) => {
axios.post(apiURL, {
email: email,
password: password
})
.then(respone => {
console.log('Promise Signup response:', respone);
resolve(respone);
}, error => {
console.log('Promise Signup error:', error);
reject(error);
});
});
}
export {
Signup
};