javascript - 当服务器端不允许 cors 时在 javascript 中发出 ajax 请求?
问题描述
- 使用 Fetch 请求
function status(response) {
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response)
} else {
return Promise.reject(new Error(response.statusText))
}
}
function json(response) {
return response.json()
}
fetch('https://apis.mapmyindia.com/advancedmaps/v1/+apikey+/autosuggest?q=delhi',{mode: 'no-cors'})
.then(status)
.then(json)
.then(function(data) {
console.log('Request succeeded with JSON response', data);
}).catch(function(error) {
console.log('Request failed', error);
});
上面的代码给出了错误
我也尝试了httprequest,但它不起作用
解决方案
它不起作用,因为您必须在服务器上启用 cors。但是,您可以为此使用代理服务器。
推荐阅读
- ios - Flutter Autofill 登录仅适用于电子邮件字段,不适用于 iOS 上的密码字段
- jenkins - 如何在 Jenkinsfile 中查找谁触发了构建
- android - 在 Play 管理中心中添加到活动封闭式测试的新测试人员无权访问
- excel - VBA 运行时错误“1004”:应用程序定义或对象定义错误”设置单元格内容
- c# - 在所有轴上旋转模型
- python - 从原点开始 x 轴刻度
- java - java - 如何在java中将十六进制反转为反转十六进制?
- python-3.x - Python在文本文件中搜索模式的有效方法
- google-chrome - 对于运行 Windows 7 的计算机,Chrome 上的 SSL 问题 NET::ERR_CERT_DATE_INVALID
- vulkan - 为什么 Vulkan 中的栅栏状态检查和重置如此缓慢?