我用 ReactJS 写了一个客户端,我用 axios 进行 http 请求,我向服务器发出一个 post 请求,请求在服务器上处理并返回数据和一个 200 代码,但是浏览器中的错误是:
从源“ http://localhost:3000 ”访问“ https://Xxxxxx ”处的 XMLHttpRequest已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头。
和警告
xhr.js:166 跨域读取阻止 (CORB) 阻止了 MIME 类型 application/json 的跨域响应“ https://Xxxxxx ”。有关详细信息,请参阅 https://www.chromestatus.com/feature/5629709824032768 。
我阅读了浏览器的建议,但没有帮助
编码:
import axios from 'axios';
const baseIP = ''
const baseURL = 'x' + baseIP
const baseAPI = axios.create({
baseURL: baseURL,
});
const getConfig = (params) => {
return {
baseURL: baseURL,
headers: {
'Content-Type': 'application/json',
},
params: params,
};
};
const API = {
// isAuth: () => { return Auth.isAuthenticated(); },
// Login: (data, handler) => { return Auth.authenticate(data, handler); },
// Logout: (handler) => { return Auth.signout(handler); },
GET: (path, params) => baseAPI.get(path, getConfig(params)),
POST: (path, data) => baseAPI.post(path, data, getConfig()),
PUT: (path, data) => baseAPI.put(path, data, getConfig()),
DELETE: (path, params) => baseAPI.delete(path, getConfig(params)),
};
class BotService{
getFonds = () => API.POST('x')
};
export default BotService;
你能建议在哪里挖吗?
这通过两种方式解决。
作为第三种解决方案 - 在服务器上代理 api 请求,同时排除 CORS。