我目前正在制作格斗游戏覆盖,因此安全性不是问题,因为它是只有我可以访问的本地文件。
我一直遇到的问题是标题中列出的问题,搜索后似乎找不到任何解决方法。
这是Javascript:
window.onload = init;
function init(){
var xhr = new XMLHttpRequest(); //AJAX data request sent to server(in this case server being local json file)
var streamJSON = '../sc/streamcontrol.json'; //specifies path for streamcontrol output json
var scObj; //variable to hold data extracted from parsed json
var startup = true; //flag for if looping functions are on their first pass or not
var animated = false; //flag for if scoreboard animation has run or not
var cBust = 0; //variable to hold cache busting value
xhr.overrideMimeType('application/json'); //explicitly declares that json should always be processed as a json filetype
function pollJSON() {
xhr.open('GET',streamJSON+'?v='+cBust,true);
xhr.send();
cBust++;
}
pollJSON();
setInterval(function(){pollJSON();},500);
xhr.onreadystatechange = parseJSON;
function parseJSON() {
if (xhr.readyState == 4 && xhr.status == 200) {
scObj = JSON.parse(xhr.responseText);
console.log(xhr.responseText);
if(animated == true) {
scoreboard();
}
}
}
function scoreboard() {
var game = scObj['game'];
var round = scObj['round'];
if(startup == true) {
$('#game').html(game);
$('#round').html(round);
}
}
setTimeout(scoreboard,300);
}
这是 JSON 代码:
{
"cTitle1": "",
"cTitle2": "",
"game": "SSBM",
"mText1": "",
"mText2": "",
"mText3": "",
"mText4": "",
"p1Name": "gamer",
"p1Score": "0",
"p1Team": "",
"p2Name": "gaming",
"p2Score": "0",
"p2Team": "",
"round": "Winner's Finals",
"timestamp": "1574024799"
}
和 HTML 代码:
<meta charset="UTF-8">
<html>
<head>
<script src="../js/read.js" type="text/javascript"></script>
<script src="../js/jquery-3.4.1.min.js" type="text/javascript"></script>
<script src="../js/greensock-js/src/minified/TweenMax.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="../css/playercam.css">
</head>
<body>
<div class="all">
<div id="main">
<div id="divContent">
<div id="rdWrapper">
<span id="game">SSBM</span>
<span id="round">Round</span>
</div>
</div>
</div>
<div class="divBot">
Stream by MegabyteMelee
</div>
</div>
</div>
<script type="text/javascript">
var rdSize = '160px'; //default font size of round container, should match 'font-size' value of #round
</script>
</body>
</html>```
Any sort of help is appreciated!