我从我的节点服务器发送了一个JSON对象,并且我在我的前端接收了它,但是我无法访问内容,这是一个我希望在DIV中显示的HTML文件,出于教育目的我正在使用纯java脚本。
我发布了我的XHR响应的图片,我可以看到该对象,但我无法显示它的内容,请帮忙!
这就是我的后端:
app.post('/searchDB', function(req, res) {
var Tutorial = require("./server/models/tutorial")
Tutorial.find({
content: new RegExp(req.body.term, "i")
}).exec(function(error, tutorials) {
console.log(tutorials)
// var searchContent = tutorials
// res.end(tutorials)
res.json({
searchresults: tutorials
})
})
})
这就是我在前端捕获它的信息,控制台日志显示的是搜索数据,这是我第一次问stackO一个问题所以我是新的,对不起,如果我没有正确解释。
前端代码:
var xhr = new XMLHttpRequest()
xhr.open("POST", "/searchDB", true)
// set a variable equal to the form data variable
var fd = new FormData()
fd.append("term", content)
//now send input value to our server in fd variable
xhr.send(fd)
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
//- console.log(xhr.responseText)
searchData = JSON.parse(xhr.responseText)
//- console.log(searchData)
console.log("the following object has been received from the server:")
console.log(searchData)