【发布时间】:2019-12-13 17:13:27
【问题描述】:
我创建了一个 UI 来使用 HTML 读取、写入和更新配置文件,但我无法将新的 JSON 对象保存到我现有的配置文件中。
所以我需要将 JSON 对象从 HTML 文件发布到 nodejs 服务器
JAVASCRIPT
var testJson = { name: "mufashid" }
$.ajax({
url: 'http://localhost:3000/test/config',
type: 'POST',
dataType: 'json',
data: { o: JSON.stringify(testJson ) }, // bags collection value what your goging to send to server
// crossDomain: true,
success: function (data) {
alert('Success!')
},
error: function (jqXHR, textStatus, err) {
//show error message
alert('text status ' + textStatus + ', err ' + err)
}
});
nodejs
app.post('/test/config', function (req, res) {
// console.log(res);
console.log(req)
})
按下保存按钮时需要使用新值更新配置文件。
【问题讨论】:
-
你试过
req.data吗?你可以尝试使用console.log来调试 -
@Shinjo 是的,我试过
req.data它显示未定义。 -
console.log(req)返回什么? -
你添加
app.use(require('express').json())了吗? -
你的 (
res) 呢?可以帮忙:stackoverflow.com/questions/45105992/…,stackoverflow.com/questions/13478464/…。一旦您能够从 ajax 请求访问您的 json,您就可以轻松地操作数据。