Access to XMLHttpRequest has been blocked by CORS policy

后端 未结 7 2226
庸人自扰
庸人自扰 2021-02-18 16:49

I\'ve a problem when I try to do PATCH request in an angular 7 web application. In my backend I have:

app.use((re         


        
7条回答
  •  臣服心动
    2021-02-18 17:08

    I ran into the same issue even though my API was using cors and had the proper headers.

    So for me, the issue was that I was making an insecure request. I was accessing my API over the http protocol, and that was causing the error.

    I just changed from

    http://api.example.com
    

    to https protocol

    https://api.example.com
    

    and it fixed everything.


    You can also create a simple proxy on your website to forward your request to the external site. For example, if you are trying to fetch some data from your website (my-website.com) to (another-website.com) and you make a POST request, you can have cors issues, but if you fetch the data from your own domain you will be good.

    Here is how to create a simple proxy forwarding the request https://stackoverflow.com/a/20354642/7602110

    So instead of making request to

    POST https://api.example.com/event/create
    

    You'll want to make the request to

    POST https://my-website.com/api/event/create
    

    I know that is some extra work, and sometimes you don't have the ability to do it, but that will definitely prevent you from having cors issues.

提交回复
热议问题