【发布时间】:2020-01-27 04:02:57
【问题描述】:
我想创建一个返回 json 数据类型和 ajax 的请求 GET
路线就这么简单:
app.get('/', function(req, res) {
res.json({ answer: 42})
});
当我在浏览器中打开 / 时,它会呈现:
一切正常,但是我试图用 XMLHttpRequest vanilla JS(没有 jquery)得到答案 json:
var xhr = new XMLHttpRequest();
xhr.open("GET", 'http://localhost:3000/');
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json'
xhr.addEventListener('load', function () {
alert(this.response) // response is 'null'
})
xhr.send();
【问题讨论】: