【发布时间】:2019-12-09 07:05:01
【问题描述】:
我在网页上使用以下 javascript 在“单击”图像时将信息发送到 Node.js 服务器。这是使用“POST”请求。
<script>
function rerouter(_sent) {
var _people = <%- JSON.stringify(member_list) %>;
//convert the passed ('member_list') array into a JSON string...
var _attend = <%- JSON.stringify(online) %>;
//convert the passed ('online') array into a JSON string...
var splits = _sent.id.split("_"); //"split" on "underscore ('_')"
if (_people.indexOf(splits[1]) != -1) {
//**SEND INFO TO SERVER...
var available = _attend[_people.indexOf(splits[1])];
var response = fetch("members/pages/:" + splits[1] + "/presence/:" + available, {
method: 'POST',
headers: {
'Content-Type': 'text/plain;charset=utf-8'
}
});
//**
} //'_people' array contains the member name ('splits[1]')...
}
</script>
在这里,我在我的 Node.js 服务器代码中处理请求:
var bodyParser = require('body-parser')
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.post('/members/pages/:membername/presence/:online', urlencodedParser, function (req, res) {
console.log("I RECEIVED FROM CLIENT THE FOLLOWING:")
console.log(req.params)
console.log(req.body)
res.redirect('/_landing');
})
这是我的控制台输出:
I RECEIVED FROM CLIENT THE FOLLOWING:
{ membername: ':Nica', online: ':Yes' }
{}
从我的输出中可以看出,POST 路由似乎在某种程度上是有效的。但是我的“重定向”命令没有执行...网页没有更改为应有的“_landing”页面...我认为这可能是因为我正在使用“获取”发送 POST 请求...? ??有人可以验证这是否是原因(或另一个问题是原因)以及我如何能够纠正该问题?
此外,当我登录到控制台时,为什么我的“参数”包含冒号(“:”)......这是标准吗?我认为它不会在日志中包含冒号,而只会包含实际数据。
基本上,我的 POST 似乎几乎可以工作了……但不完全是。任何建议将不胜感激。提前谢谢你。
更新:我做了一些更改,我的 POST 现在似乎工作正常。在我的前端网页中,我使用以下内容发出 HTTP POST 请求:
<script>
function rerouter(_sent) {
var _people = <%- JSON.stringify(member_list) %>;
//convert the passed ('member_list') array into a JSON string...
var _attend = <%- JSON.stringify(online) %>;
//convert the passed ('online') array into a JSON string...
var splits = _sent.id.split("_"); //"split" on "underscore ('_')"
if (_people.indexOf(splits[1]) != -1) {
//**SEND INFO TO SERVER...
var available = _attend[_people.indexOf(splits[1])];
fetch('/members/pages/callup', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({name: splits[1], presence: available, str: 'Some string: &=&'})
})
//**
} //'_people' array contains the member name ('splits[1]')...
}
</script>
...并在我的 Node.js 脚本中修改了我的路由处理程序:
// create application/json parser
var jsonParser = bodyParser.json()
app.post('/members/pages/callup', jsonParser, function (req, res) {
console.log("I RECEIVED FROM CLIENT THE FOLLOWING:")
console.log(req.body)
res.redirect('/_landing');
})
这是功能...接收从前端网页发送的数据。
唯一剩下的问题是为什么“重定向”没有触发......???我仍然有一种感觉,通过使用“获取”,这会以某种方式干扰页面重定向......?提取通常用于等待来自服务器的响应,在我的情况下,我对那个功能不感兴趣,我只想从前端向后端单向发送数据......然后重定向前端页面。我想不出重定向不触发的任何其他原因......?
【问题讨论】:
-
你的
app.js(快递代码)中有body-parser模块吗? -
是的,我愿意...我应该将它包含在我的路线中吗? 'app.post('/members/pages/:membername/presence/:online', urlencodedParser, function (req, res) {' ...其中 'urlencodedParser' 定义为 'var urlencodedParser = bodyParser.urlencoded ({extended: false })'...?? 感谢您的回复。
-
我试了一下......我看到的唯一区别是'req.body'控制台日志不再是'未定义'......它只是空的('{}') ...我在上面编辑了我的帖子以反映我所做的更改
-
@panjit 在下面查看我的答案。