Accepted answer

your server should enable the cross origin requests, not the client. to do this, you can check this nice page with implementations and configurations for multiple platforms


cors issue can be simply resolved by following this:

create a new shortcut of google chrome(update browser installation path accordingly) with following value:

"c:\program files (x86)\google\chrome\application\chrome.exe" --disable-web-security --user-data-dir="d:\chrome\temp"


just simply add this to your headers

headers : {
    'content-type' : 'application/x-www-form-urlencoded; charset=utf-8'

no need to use access-control-allow-origin : *


please try this .. it worked for me

axios.get(`http://localhost:4000/api`,{ crossdomain: true }).then((result)=>{


i come across this thread when having the same problem using axios. what was not mentioned in the responses is that using fetch with no-cors mode can solve your issue.

why ?

apparently, axios uses a xmlhttprequest under the hood, not request and axios fails because cors is still being enforced and no-cors mode is not supported.

check this thread for more information


this work out for me :

in javascript :

            method: 'post',
            headers: { 'content-type': 'application/x-www-form-urlencoded' },
            url: 'https://localhost:44346/order/order/giveorder',
            data: order
          }).then(function (response) {

and in the backend (.net core) : in startup:

 #region allow-orgin
            services.addcors(c =>
                c.addpolicy("alloworigin", options => options.allowanyorigin());

and in controller before action



cors issue is something you will only encounter on a broswer. it occurs beacuse the server does not allow request from others servers

i.e if i am sending request from http://localhost:3000 to any api( to get the user data from here.

if the server does not recognize your local host

@crossorigin(origin = "*") // this will allow any request from any server you will not face cors issue if you us this annotation

now what if you are sending a request using axios in react to another sever which is not in your control the way to overcome that issue is by using http-proxy-middleware

npm i http-proxy-middleware // install this dependency

axios.{ method: 'post', url: '/endpoint', headers: { 'content-type': 'application/json', }, proxy: createproxymiddleware({ target: '', changeorigin: true}), data: data };

now in this way a proxy request to will be sent and thus you will not recieve a cors issue

also add this in your package.json

"proxy": ""


i had got the same cors error while working on a vue.js project. you can resolve this either by building a proxy server or another way would be to disable the security settings of your browser (eg, chrome) for accessing cross origin apis (this is temporary solution & not the best way to solve the issue). both these solutions had worked for me. the later solution does not require any mock server or a proxy server to be build. both these solutions can be resolved at the front end.

you can disable the chrome security settings for accessing apis out of the origin by typing the below command on the terminal:

/applications/google\\ chrome --user-data-dir="/tmp/chrome_dev_session" --disable-web-security

after running the above command on your terminal, a new chrome window with security settings disabled will open up. now, run your program (npm run serve / npm run dev) again and this time you will not get any cors error and would be able to get request using axios.

hope this helps!



this is happening because of restrict-origin-when-cross-origin policy.browser sends a pre-flight request to know whom the api server wants to share the resources. so you have to set origin there in api server and send some status.after that the browser allow to send the request to the api server.

here is the code.i am running front-end on localhost:8000 and api server is running on port 6000.

const cors = require("cors");

app.options("*", cors({ origin: 'http://localhost:8000', optionssuccessstatus: 200 }));

app.use(cors({ origin: "http://localhost:8000", optionssuccessstatus: 200 }));

i have set origin as my front-end url, if you set it to true , then it will allow only port 8000 to access rosource, and front-end running on port 8000 can not access this resource. use this middleware before route in api server.


i have encountered with same issue. when i changed content type it has solved. i'm not sure this solution will help you but maybe it is. if you don't mind about content-type, it worked for me.['content-type'] ='application/x-www-form-urlencoded';


just noting my solution for someone who might get here from googling. i resolved my cors issue (when calling an external api from my ui in the browser) by setting withcredentials to false in my axios call:

    method: 'get',
    url: ``,
    withcredentials: false,
    params: {
      access_token: secret_token,

in this case, the external api's endpoint's security is based on the access_token.


may be helpful to someone:

i'm sending data from a react application to a golang server.

once i change this, w.header().set("access-control-allow-origin", "*"), the error was fixed.

react form submit function:

async handlesubmit(e) {
    const headers = {
        'content-type': 'text/plain'

            user_name: this.state.user_name,
            password: this.state.password,
        ).then(response => {
            console.log("success ========>", response);
        .catch(error => {
            console.log("error ========>", error);

go server got router,

func main()  {
    router := mux.newrouter()

    router.handlefunc("/login", login.login).methods("post")

    log.fatal(http.listenandserve(":3001", router))


func login(w http.responsewriter, r *http.request)  {

    var user = models.user{}
    data, err := ioutil.readall(r.body)

    if err == nil {
        err := json.unmarshal(data, &user)
        if err == nil {
            user = postgres.getuser(user.username, user.password)
            w.header().set("access-control-allow-origin", "*")

Related Query

More Query from same tag