[英]How to make an AJAX request to get a word and store the variable globally
我需要向API發出 AJAX 請求,以獲得劊子手游戲的消息。 在閱讀單詞長度並用占位符顯示它時也很掙扎。 我引用了這個鏈接在此處輸入鏈接描述,但仍然卡住。
//function to get the word from the API using AJAX
jQuery.extend({
getValues: function(url) {
var result = null;
$.ajax({
url: url,
type: 'get',
dataType: 'xml',
async: false,
success: function(data) {
result = data;
}
});
return result;
}
});
//accessing global variable (word)
var results = $.getValues("https://hangman-api.lively.software");
for (var i < 0; i < word.length; i++) {
results[i] = "_";
}
希望我的片段可以在某些方面幫助你。 祝你今天過得愉快!
$(function(){ $.ajax({ url: "https://hangman-api.lively.software/", type: 'get', success: function(data) { //how to access word console.log(data.word) //how to create placeholders placeHolder = " _ "; placeHolders = placeHolder.repeat(data.word.length) //how to print place holder $(".placeHolder").text(placeHolders) } }); })
<html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> </head> <body> Placeholders: <span class="placeHolder"></span> </body> </html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.