Node JS write response
Node JS write response
不知道如何给服务器写一个响应。
现在我有一个登录表单发送数据到服务器。在服务器端,我检查数据是否与。json文件中的数据具有相同的值,如果一切正确,我想发送响应。
if (login == "login") { // if POST request comes wiht 'login' parameter
fs.readFile("JSON/DB.json", "utf8", function (err, data) {
var jsonFileArr = []; // Data from .json
jsonFileArr = JSON.parse(data);
var logPost = loginData.log; // 'log' data from request
var gotData = jsonFileArr.find(function (obj) {
// Search for the same 'log' data in .json
return obj.log === logPost;
});
if (gotData === undefined) { // No same log
console.log("ERROR: Wrong 'log' or 'pass'")
}
else if (gotData.log == logPost) { // if there is the same 'log' ,
// check for same 'pass'
if (gotDaten.pass == passPost) { // Same 'pass' found , send the response
console.log("Send Response");
response.writeHead(200, { 'Content-Type': 'application/json', });
var resObject = { "status": "OK" };
var json = JSON.stringify(resObject);
response.end(json);
console.log(json);
}
else
console.log("ERROR: Wrong 'log' or 'pass' ");
}
});
}
else {
console.log("Wrong request");
}
响应没有被发送,也许我如何设置节点http服务器有问题。如何正确编写服务器响应?
当我尝试使用response.write()
时,它会给我一个write after end
错误。
这是整个Node JS服务器:jsfiddle
找到了问题,我只需要移动我的代码到页面被调用的地方,并使else if
循环而不是if
您可以将response.setHeader(name, value)
与res.send(data)
一起使用
试试这个
if (login == "login") { // if POST request comes wiht 'login' parameter
fs.readFile("JSON/DB.json", "utf8", function(err, data) {
var jsonFileArr = []; // Data from .json
jsonFileArr = JSON.parse(data);
var logPost = loginData.log; // 'log' data from request
var gotData = jsonFileArr.find(function(obj) {
// Search for the same 'log' data in .json
return obj.log === logPost;
});
if (gotData === undefined) { // No same log
console.log("ERROR: Wrong 'log' or 'pass'")
} else if (gotData.log == logPost) { // if there is the same 'log' ,
// check for same 'pass'
if (gotDaten.pass == passPost) { // Same 'pass' found , send the response
console.log("Send Response");
response.setHeader(
'Content-Type', 'application/json');
response.status(200);
var resObject = {
"status": "OK"
};
var json = JSON.stringify(resObject);
response.send(json);
console.log(json);
} else
console.log("ERROR: Wrong 'log' or 'pass' ");
}
});
} else {
console.log("Wrong request");
}
如果您使用express,您可以直接使用response.json(json)
相关文章:
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 在Kentico中使用response.write重定向HTML表单不起作用
- 如何在 Response.Write 中格式化字符串
- 我如何通过方法写入数据 response.write 从 MySQL 服务器到 Node.js 程序后
- 通过 Response.Write() 写回函数
- 如何在express.bodyParser()和response.write()中使用node
- 当我通过Jquery.post()传递主函数时,Response.Write函数不起作用
- Node.js - response.write(...) not working
- Response.write()没有返回值
- Ajax请求返回整个页面,而不是在Response.Write中发送文本
- Node JS write response
- response.write在回调函数中
- Response.write(script) is not working.
- asp.net response.write with bootbox.alert
- 如何使用response.write从javascript调用vb脚本
- 为什么无法使用response.writeHead和response.write编写响应
- 将文件发送到浏览器/response.write备选方案
- 为什么当我使用asp.net中的Response.Write()方法输出javascript代码时,页面中的css样式会
- ASP.NET 将 Response.Write 与 JavaScript 相结合的编程
- Response.write()将不接受字符串.它的行为就好像它是未定义的