AJAX供电的数据驱动应用

When I type in an artist and I click the button, I should get a nice list of their songs in a format that is aesthetically pleasing.

My error is: Nothing is returns whenever I press the button, I don't know why that is.

HTML File

<!DOCTYPE html>
<html>
<head>
    <title>Songs</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet"     href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script
    src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
    </script>
    <script
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
    </script>
    <script src="ajaxfunctions.js"></script>
</head>
<body>
    <div class="container">
        <h2>Songs </h2>
        <br/>
        <p>Try entering an artist to find their songs</p>
        <div>
            <div id="musicbox" class="control-group">
                <label for="songs">artistr</label>
                <input type="text" name="songs" id="songs" placeholder="Type an artist to their songs" />
            </div>
        </div>
        <br />
        <div>
            <button class='btn btn-success btn-large' onclick="Music('songs')">Find Songs</button>
        </div>
        <br />
        <div>
        </div>
    </div>
</body>
</html>

JS File

function Music(music) {
    var yourmusic = document.getElementById(music).value;
    var httpRequest = new XMLHttpRequest();
    httpRequest.onreadystatechange = function () {
        if (this.readyState === 4) {
            if(this.status === 200) {
                displayMusic(this.responseText);
            } else if (this.status === 404){
                displayMusic('{ "songs" : "none" }');
            } else {
                console.log("We have a problem: " + this.status);
            }
        } else {
        }
    };
    var url = " https://api.mixcloud.com/discover/funk/";
    httpRequest.open("GET", url, true);
    httpRequest.send();
}

function displayMusic(data){
    var music = JSON.parse(data);
    if(music.songs === "none") {
        document.getElementById("songs").className = "alert alert-warning";
        document.getElementById("songs").innerHTML = "The songs are" + music;
    } else {
        document.getElementById("songs").className = "alert alert-success";
        document.getElementById("songs").innerHTML ="there are no songs for this artist"
    }
}
查看全部
weixin_33698823
weixin_33698823
2017/06/06 23:50
  • getelementbyid
  • ajax
  • javascript
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信

0个回复