这就是我要找出的。 如何用纯JavaScript编写$ .get(url,function(data){code};。
function newimage(keyword){
if(!ACCESS_KEY){
alert("Please update your access key");
return;
}
var url = `https://api.unsplash.com/search/photos?query=${keyword}&per_page=20&orientation=landscape&client_id=${ACCESS_KEY}`;
$.get(url,function(data){
var picture = data.results[0];
var picture_url = picture.urls.raw;
var photo_by_name = picture.user.name;
var photo_by_url = picture.user.links.html;
setCookie("picture",picture_url,0.5);
setCookie("photo-by-name",photo_by_name,0.5);
setCookie("photo-by-url",photo_by_url,0.5);
picInterest.innterHTML = `${keyword}`;
photoBy.innterHTML = `${photoByUrl}`;
photoBy.setAttribute('html', photoByUrl);
document.querySelector("body").style.backgroundImage = `url(${pictureUrl})`;
pictureOption.style.display = "block";
});
}
答案 0 :(得分:1)
您可以像这样使用提取API:
fetch(url).then(res => res.json()).then(data => {
//whatever you want
})
答案 1 :(得分:0)
您可以使用此:
var url = `https://api.unsplash.com/search/photos?query=${keyword}&per_page=20&orientation=landscape&client_id=${ACCESS_KEY}`;
var xhr = new XMLHttpRequest ();
xhr.open ( "GET", url);
xhr.onreadystatechange = function ()
{
if ( xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200)
{
// Here you handle the response
// Result text will be accessible at xhr.responseText
}
}
xhr.send ();