XMLHttpRequest.readyState & XMLHttpRequest.status

我有一个脚本,我想看看是否可以修复比较。


this.refreshLyric = function (currentSong, currentArtist) {

    

     var xhttp = new XMLHttpRequest();

        xhttp.onreadystatechange = function () {

            if (this.readyState === 4 && this.status === 200) {

                var data = JSON.parse(this.responseText);


                var openLyric = document.getElementsByClassName('lyrics')[0];


                if (data.type === 'exact' || data.type === 'aprox') {

                    var lyric = data.mus[0].text;


                    document.getElementById('lyric').innerHTML = lyric.replace(/\n/g, '<br />');

                    //debugging

                    console.log("Success Lyric found");

                    

                } else {

                //debugging

                    console.log("Lyric not found");

                }

            } else {

               //HERE if the condition is not met, it goes to another function

               var page = new Page();

               page.refreshLyric2(currentSong, currentArtist);

              }

        }

        xhttp.open('GET', 'https://api.vagalume.com.br/search.php?apikey=' + API_KEY + '&art=' + currentArtist + '&mus=' + currentSong.toLowerCase(), true);

        xhttp.send()

}

代码很简单,但是我想要的却无法实现。


这种比较是真实的必须经过一些先前的状态:


if (this.readyState === 4 && this.status === 200) {


XMLHttpRequest.readyState:


值状态说明


0 UNSENT 客户端已创建。open() 尚未调用。


1 OPENED 已调用 open()。


2 HEADERS_RECEIVED send() 已被调用,并且标头和状态可用。


3 LOADING 下载;responseText 保存部分数据。


4 DONE 操作完成。


XMLHttpRequest.status:


在请求完成之前,status 的值为 0。如果出现 XMLHttpRequest 错误,浏览器也会报告状态 0。

未发送:0

开业:0

加载:200

完成:200

我想做的是,如果状态的最后阶段比较分别不等于 4 和 200,则转到另一个函数。


if (this.readyState === 4 && this.status === 200) {

//run this code

.....

 } else {

//Go to another function

   var page = new Page();

   page.refreshLyric2(currentSong, currentArtist);

 }

有可能实现这一目标,还是我在做白日梦?


肥皂起泡泡
浏览 116回答 1
1回答

慕慕森

如果您希望根据状态在最后阶段执行不同的操作,则需要嵌套if语句。首先if检测最后阶段,然后测试状态。this.refreshLyric = function(currentSong, currentArtist) {&nbsp; var xhttp = new XMLHttpRequest();&nbsp; xhttp.onreadystatechange = function() {&nbsp; &nbsp; if (this.readyState == 4) {&nbsp; &nbsp; &nbsp; if (this.status == 200) {&nbsp; &nbsp; &nbsp; &nbsp; var data = JSON.parse(this.responseText);&nbsp; &nbsp; &nbsp; &nbsp; var openLyric = document.getElementsByClassName('lyrics')[0];&nbsp; &nbsp; &nbsp; &nbsp; if (data.type === 'exact' || data.type === 'aprox') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var lyric = data.mus[0].text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('lyric').innerHTML = lyric.replace(/\n/g, '<br />');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //debugging&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Success Lyric found");&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //debugging&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Lyric not found");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // go to another function&nbsp; &nbsp; &nbsp; &nbsp; var page = new Page();&nbsp; &nbsp; &nbsp; &nbsp; page.refreshLyric2(currentSong, currentArtist);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; xhttp.open('GET', 'https://api.vagalume.com.br/search.php?apikey=' + API_KEY + '&art=' + currentArtist + '&mus=' + currentSong.toLowerCase(), true);&nbsp; xhttp.send()}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript