我想向外部URL发出请求,并处理此URL的内容。URL的内容包含纯文本,但它有大约50,000个字符。
所以我搜索了一些关键字,比如: Ajax请求、get请求、异步/同步、回调等等。
但所有这些解决方案都是复杂的。
如何才能成为一个简单的例子,具备以下标准:
谢谢。
更新:
请注意,示例URL更改为新URL。
当我运行一个GET请求(例如使用这个在线获取请求工具)时,我就能够读取响应内容。这不像以前下载任何东西,只是显示内容。这正是我想要做的。只需将此URL的“响应内容”写入JS中的一个变量,并在以后使用它。
发布于 2018-10-26 23:44:58
很简单
let request = new XMLHttpRequest();
request.open("GET", "http://www.example.org/example.txt", true);
request.onload = () => {console.log(request.responseText)}
request.send();发布于 2018-10-26 23:52:15
你可以试试这个https://developers.google.com/web/updates/2015/03/introduction-to-fetch
fetch('https://api.lyrics.ovh/v1/shakira/waka-waka')
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' +
response.status);
return;
}
// Examine the text in the response
response.json().then(function(data) {
console.log(data);
});
}
)
.catch(function(err) {
console.log('Fetch Error :-S', err);
});
发布于 2018-10-27 00:23:20
您可以使用超剂,它比jQuery轻,比其他解决方案更兼容。你也可以找到一些例子
https://stackoverflow.com/questions/53017467
复制相似问题