如果Ajax的ResponseText返回的是true但无法写入到div中,可能是因为ResponseText返回的是一个布尔值,而不是一个可以直接输出到div中的文本内容。以下是一种解决方法:
HTML代码:
JavaScript代码:
var xhr = new XMLHttpRequest();
xhr.open("GET", "your_api_url", true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = xhr.responseText;
if (response === "true") {
document.getElementById("myDiv").innerHTML = "true";
} else {
document.getElementById("myDiv").innerHTML = "false";
}
}
};
xhr.send();
上述代码通过XMLHttpRequest对象发送一个GET请求到指定的API URL,并在收到响应后判断responseText的值是否为"true"。如果是"true",则将"true"写入到id为"myDiv"的元素中;如果不是"true",则写入"false"。
需要注意的是,上述代码是一个简化的示例,实际情况中可能需要根据具体的需求进行修改。