此问题通常与后端服务器有关,但是如果您无权访问服务器,则有两种选择
使用此chrome扩展程序的第一个选项:Allow-Control-Allow-Origin,但是很遗憾,此扩展程序在其他浏览器中不可用,因此您需要使用
使用在线CORS代理的第二种选择
https://cors-anywhere.herokuapp.com/http://example.com
http://cors-proxy.htmldriven.com/?url=http://www.htmldriven.com/sample.json
对于需要绕过与执行对第三方服务的标准AJAX请求有关的同源策略的开发人员,CORS代理是一项免费服务。
这是使用CORS代理进行Axiox调用的示例
const urlProxy = 'https://cors-anywhere.herokuapp.com/http://example.com';
export function post() {
let users = {
username: '',
password: '',
};
return axios({
method:'POST',
url:urlProxy,
data: users, // Delete it if you dont have a data
withCredentials: true, // Delete it if your request doesn't required credentials
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
'Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Origin': '*',
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
}
Run Code Online (Sandbox Code Playgroud)
我添加了withCredentials()它,使您的浏览器在XHR请求中包含cookie和身份验证标头。如果您的服务依赖于任何cookie(包括会话cookie),则仅在使用此选项集的情况下才能使用。
有一个Firefox的扩展,增加了CORS头到3月5日发布的(编译36.0.1),任何HTTP响应在最新的Firefox工作2015年 入住此链接
希望能帮到你
| 归档时间: |
|
| 查看次数: |
12931 次 |
| 最近记录: |