FCC数据暂存

  1. random quote
    <div class="main">
    <h1>Random Quote</h1>
    <div class="wrap">
    <div class="content">
    <div class="quote random-color">"说什么呢"</div>
    <div class="author random-color">——张大侠</div>
    <div class="btn-wrap">
    <button type="button" class="twitter link-icon random-color"><img src="img/twitter.png" alt=twitter""></button>
    <button type="button" class="tumblr link-icon random-color"><img src="img/tumblr.png" alt="tumblr"></button>
    <button type="button" class="get-random random-color"><span class="new-quote">New quote</span></button>
    </div>
    </div>
    </div>
    </div>

js部分

var ajaxRequest ;
function createRequest(){
var request
try {
request = new XMLHttpRequest();
} catch(tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch(otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
console.log("失败");
return null;
}
}
}
return request;
}

function stateChange(request) {
if (ajaxRequest.readyState === 4) {
if (ajaxRequest.status === 200) {
var r = JSON.parse(ajaxRequest.responseText);
var quote = r.quote;
var author = r.author;
$(".disp-text").html = quote;
}
else {
alert("错误");
}
}
}
function stateChange() {
if (ajaxRequest.readyState === 4) {
if (ajaxRequest.status === 200) {
var r = JSON.parse(ajaxRequest.responseText);
var quote = r.quote;
var author = r.author;
$("p").html(quote);
console.log(quote);
}
else {
alert("错误");
}
}
}

$(document).ready(function(){
$("#get-info").on("click", function(){
ajaxRequest = new XMLHttpRequest();
var url = "https://andruxnet-random-famous-quotes.p.mashape.com/cat=";

    ajaxRequest.open("GET", url, true);
    ajaxRequest.setRequestHeader( "X-Mashape-Key", "OivH71yd3tmshl9YKzFH7BTzBVRQp1RaKLajsnafgL2aPsfP9V");
    ajaxRequest.setRequestHeader("Accept", "application/json");
    ajaxRequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded");

    ajaxRequest.onreadystatechange = stateChange;
    ajaxRequest.send(null);
});

});

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容