在JavaScript中进行跨域通信时,可以使用以下方法:
function getData(callback) {
var script = document.createElement('script');
script.src = 'http://example.com/data?callback=' + callback;
document.body.appendChild(script);
}
function processData(data) {
console.log(data);
}
getData('processData');
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/data', true);
xhr.withCredentials = true;
xhr.onload = function() {
console.log(xhr.responseText);
};
xhr.send();
fetch('http://example.com/proxy/data')
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
这些方法可以帮助JavaScript在跨域通信中进行数据交换。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:JavaScript跨域访问怎样实现