ajax测试报错Access to XMLHttpRequest at '' from origin '' has been blocked by CORS policy: No 'Access-Co

报错全文:Access to XMLHttpRequest at 'http://localhost:8080/' from origin 'http://127.0.0.1:8848' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

报错原因:本地不同端口也属于不同的域,因此需要跨域

实现跨域:通过script的src属性,或通过信任的服务器代理请求
 

可如图安装koa解决跨域问题

ajax测试报错Access to XMLHttpRequest at '' from origin '' has been blocked by CORS policy: No 'Access-Co_第1张图片

之后引入即可:

const cors = require("@koa/cors");

app.use(cors())

你可能感兴趣的:(web前端基础)