此代码用于发送JSON请求并将其存储在变量json下。然后,它意味着根据键的值显示图片(特别是如果关键性别=男性)。没有图片显示,我无法在任何地方找到解决方案,任何人都可以提供帮助吗?
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;
}
}}
&#13;
答案 0 :(得分:0)
您的代码存在很多问题,最重要的是您似乎误解了AJAX的工作原理。这是我的修复,其中包含对错误以及在哪里的评论。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0.1,4,0.5)
y1 = np.exp(-x)
y2 = np.exp(-x)*1.2+0.25
plot = plt.figure()
ax = plot.add_axes([0,0,1,1])
ax.plot(x,y1,color="red",label="Y1")
ax.errorbar(x,y2,yerr=0.1,color="blue",marker='*',capsize=4,label="Y2")
leg = ax.legend();
for artist, text in zip(leg.legendHandles, leg.get_texts()):
col = artist.get_color()
if isinstance(col, np.ndarray):
col = col[0]
text.set_color(col)
plt.show()
答案 1 :(得分:-1)
试试这个:
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 j = 0; j < json.length; j++){
if(json[i].hasOwnProperty(gender) && json[i][gender] === male) {
img= document.createElement("img");
img.setAttribute("src", "http://advertdemo.ga/adverts/emotion_neutral/male/young/iphone.jpg");
img.setAttribute("width", "300");
img.setAttribute("height", "300");
// This next line will just add it to the <body> tag
document.body.appendChild(img);
break;
}
}}