2017-06-02 2 views
0

Ich habe Probleme beim Einrichten des Codes. Wenn Sie ein Künstler eingeben und die Taste drücken, es soll alleUncaught TypeError: Kann Eigenschaft 'Klassenname' von Null nicht festlegen Fehler bei Konsole

ihre Songs

JavaScript-Datei geben:

/** 
* Uses AJAX to query an internet data source for zip codes 
* @param {string} zipId The element id that has the zip code 
*/ 
function Music(music) { 
// First get the zip code from the HTML textbox 
var yourmusic = document.getElementById(music).value; 
// Now make a HTTP request 
var httpRequest = new XMLHttpRequest(); 
httpRequest.onreadystatechange = function() { 
    if (this.readyState === 4) { 
     // We got a response from the server! 
     if(this.status === 200) { 
      // The request was successful! 
      displayMusic(this.responseText); 
     } else if (this.status === 404){ 
      // No postal code found 
      displayMusic('{ "songs" : "none" }'); 
     } else { 
      console.log("We have a problem: " + this.status); 
     } 
    } else { 
     // Waiting for a response... 
    } 
}; 
// Notice how the URL is appended with the zip code 
var url = " https://api.mixcloud.com/discover/funk/"; 
httpRequest.open("GET", url, true); 
httpRequest.send(); 
} 
/** 
* Displays the zip code place given the JSON data 
* @param {string} data JSON data representing place for given zip 
code 
*/ 
function displayMusic(data){ 
var music = JSON.parse(data); 
if(music.songs === "none") { 
    document.getElementById("").className = "alert alert-warning"; 
    document.getElementById("").innerHTML = "The songs are" + music; 
} else { 
    document.getElementById("music").className = "alert alert-success"; 
    document.getElementById("music").innerHTML ="there are no songs for this    artist" 
} 
} 

HTML-Datei:

<!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> 

+0

aber es ist ziemlich offensichtlich, dass es nicht ein Element mit der ID „Musik“ – jdmdevdotnet

+0

auch nicht finden, was das ist? 'document.getElementById (" ")' das macht keinen Sinn. – jdmdevdotnet

+0

meine Güte, dauert 1 Minute, um einen Codepen zu machen –

Antwort

1

Der Selektor document.getElementById("") in diesen Zeilen

document.getElementById("").className = "alert alert-warning"; 
document.getElementById("").innerHTML = "The songs are" + music; 

wird immer null zurückgeben! Die erste Zeile verursacht den Fehler Uncaught TypeError: Cannot set property 'className' of null. Wahrscheinlich sollten Sie das nicht tun. Sie müssen diese zwei Zeilen entfernen und Ihre Logik überdenken.

Verwandte Themen