0

I wrote a page and it can be visited in sheyh.cn/multi/#/singleText. I tried to make a request to https://aip.baidubce.com/oauth/, It works properly in Vue devserver proxy and the config is like this

 devServer: {
    port: "8082",
    proxy: {
      "/oauth": {
        target: "https://aip.baidubce.com",
        changeOrigin: true,
        secure: false,
      },
    },
  },

After I upload the file to server the CORS error occured.

Then I tried to config my Nginx proxy like this.


#PROXY-START/multi

location /oauth/
{
    add_header Access-Control-Allow-Origin '*';
    add_header Access-Control-Allow-Methods 'POST,PUT,GET,DELETE';
    proxy_pass https://aip.baidubce.com/;
    proxy_set_header Host aip.baidubce.com;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;
    #Set Nginx Cache

    set $static_filefFIlksdb 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
        set $static_filefFIlksdb 1;
        expires 1m;
    }
    if ( $static_filefFIlksdb = 0 )
    {
        add_header Cache-Control no-cache;
    }
}
#PROXY-END/

The error is still there Access to XMLHttpRequest at 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=DiChTG01AUDydg4gUfb88f03&client_secret=zdU2g9ZLAG9w2owWgwPEaGfGUrWHmB8j' from origin 'https://sheyh.cn' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I've added Access-Control-Allow-Origin in my config, but it doesn't work properly.

Sheyh
  • 13
  • 1

1 Answers1

0

This is a Access-Control-Allow-Origin error. The reason for this error is that you have accessed the domain name https://aip.baidubce.com under the domain name https://sheyh.cn. You can check the code, it should be called directly https://aip.baidubce.com/oauth/2.0/token This interface.

It is recommended that you call https://aip.baidubce.com/oauth/2.0/token to use jsonp.

chinese:

这是一个跨域错误,产生这个错误的原因是因为你在 https://sheyh.cn 这个域名下访问了 https://aip.baidubce.com 这个域名。你可以检查一下代码,应该是直接调用了https://aip.baidubce.com/oauth/2.0/token 这个接口。

建议你调用 https://aip.baidubce.com/oauth/2.0/token 这个接口使用 jsonp.

Gleen
  • 83
  • 6