This code is meant to send a JSON request and store it under the variable json. It is then meant to display a picture depending on the value of a key (specifically if the key gender = male). No picture is displaying and I can't find a solution anywhere so is anyone able to help?
var i=20
while (i<10)
{var json = new XMLHttpRequest();
json.open('GET', "http://localhost:8081/getPersons?format=json", true);
json.send();
 
json.onreadystatechange = processRequest;
function processRequest(e) {
    if (json.readyState == 4 && json.status == 200) {
        var response = JSON.parse(json.responseText);
        alert(response.ip);
    }
}
var wantedKey = 'gender'; // your key here
var wantedVal = 'male'; // your value here
for(var i = 0; i < json.length; i++){
   if(json[i].hasOwnProperty(gender) && json[i][gender] === male) {
    document.createElement("img");
    img.src = "http://advertdemo.ga/adverts/emotion_neutral/male/young/iphone.jpg";
    img.width = "300px";
    img.height = "300px";
    // This next line will just add it to the <body> tag
    document.body.appendChild(img);
    break;
   }
}} 
     
    