繁体   English   中英

当服务器端不允许使用 cors 时,在 javascript 中发出 ajax 请求?

[英]Making ajax request in javascript when cors is not allowed from serverside?

  • 使用 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。 但是,您可以为此使用代理服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM