<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<meta name="referrer" content="never" />
<title>CORS 跨域请求,proxy</title>
</head>
<body>
<ul>
<li>支持跨域请求(转换不支持跨域请求的接口),可直接发起 ajax、fetch</li>
<li>支持HTTPS(解决远程数据接口不支持HTTPS)</li>
<li><a href="https://cors.eu.org" target="_blank">https://cors.eu.org</a></li>
</ul>
<button type="button">点我调用</button>
<input placeholder="输入url地址" value="https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN" style="width: 60%" />
</body>
</html>
body {
color: deeppink;
}
textarea {
width: 90vw;
height: 20em;
margin-top: 1em;
}
document.querySelector("button").addEventListener("click", async function () {
let domtxtUrl = document.querySelector("input");
let url = domtxtUrl.value || domtxtUrl.defaultValue;
let proxyUrl = `https://cors.eu.org/${encodeURIComponent(url)}`;
let resp = await fetch(proxyUrl);
console.debug(resp);
let text = await resp.text();
let domTxtResult = document.createElement("textarea");
try {
domTxtResult.value = JSON.stringify(JSON.parse(text), null, 4);
} catch (ex) {
domTxtResult.value = text;
}
document.body.appendChild(domTxtResult);
});