使用此代码,我创建了一个函数,该函数创建一些 JSON 数据并将其发送到 server.js(我的 Node 服务器)。
function deleteEmail(i) {
emailObj.splice(i, 1);
var general = {};
var table = []
general.table = table;
for (var i = 0; i < emailObj.length; i++) {
var dataHtml = emailObj[i].html;
var html = {
"html": dataHtml
}
general.table.push(html);
}
console.log(JSON.stringify(general));
$.post("email2", general);
}
server.js 中用于捕获 JSON 数据的代码如下:
app.post("/email2", function(req, res){
console.log(req.body);
});
这是该函数创建并发送到 server.js 的 JSON 数据:
{"table":[{"html":"<b>ID email:</b> #1481145671503<br><b>Form journey:</b> General<br><b>Work Request Title:</b> <br><b>Data Request:</b> <br><b>Request By:</b> <br><b>Department:</b> <i>not specified</i><br><b>Business Owner:</b> <br><b>Contact Details:</b> <br><b>Request Overview:</b> <br><b>Platform Impacted:</b> undefined<br><b>Business Objectives:</b> <br><b>Business Benefits Justifications:</b> <br><b>Project Drive:</b> <br><b>Additional Information:</b> <br>"},{"html":"<b>ID email:</b> #1481214851188<br><b>Date:</b> Thu Dec 08 2016 16:34:11 GMT+0000 (GMT)<br><b>Form journey:</b> General<br><b>Work Request Title:</b> <br><b>Data Request:</b> <br><b>Request By:</b> <br><b>Department:</b> <i>not specified</i><br><b>Business Owner:</b> <br><b>Contact Details:</b> <br><b>Request Overview:</b> <br><b>Platform Impacted:</b> undefined<br><b>Business Objectives:</b> <br><b>Business Benefits Justifications:</b> <br><b>Project Drive:</b> <br><b>Additional Information:</b> <br>"}]}
这是服务器正在接收的 JSON 数据:
{ 'table[0][html]': '<b>ID email:</b> #1481145671503<br><b>Form journey:</b> General<br><b>Work Request Title:</b> <br><b>Data Request:</b> <br><b>Request By:</b> <br><b>Department:</b> <i>not specified</i><br><b>Business Owner:</b> <br><b>Contact Details:</b> <br><b>Request Overview:</b> <br><b>Platform Impacted:</b> undefined<br><b>Business Objectives:</b> <br><b>Business Benefits Justifications:</b> <br><b>Project Drive:</b> <br><b>Additional Information:</b> <br>',
'table[1][html]': '<b>ID email:</b> #1481214851188<br><b>Date:</b> Thu Dec 08 2016 16:34:11 GMT+0000 (GMT)<br><b>Form journey:</b> General<br><b>Work Request Title:</b> <br><b>Data Request:</b> <br><b>Request By:</b> <br><b>Department:</b> <i>not specified</i><br><b>Business Owner:</b> <br><b>Contact Details:</b> <br><b>Request Overview:</b> <br><b>Platform Impacted:</b> undefined<br><b>Business Objectives:</b> <br><b>Business Benefits Justifications:</b> <br><b>Project Drive:</b> <br><b>Additional Information:</b> <br>' }
为什么要这样做以及在发送它并由 server.js 解释它时如何保持完全相同的格式和数据。
谢谢
最佳答案
您永远不会将 JSON 数据发送到服务器。
console.log(JSON.stringify(general));
在上面的行中,您:
- 以
general
为例,一个 JavaScript 对象 - 将其转换为 JSON
- 记录它
$.post("email2", general);
在上面的行中,您将 general
(仍然不是 JSON!)传递给 $.post
。
当您传递 post
对象时,jQuery 会将其转换为 application/x-www-form-urlencoded
编码数据并发送。
如果你想发送 JSON,那么你需要:
- 假设您正在发送 JSON
- 实际发送 JSON
例如:
$.ajax({
url: "email2",
method: "POST",
data: JSON.stringify(general),
contentType: "application/json"
});
关于javascript - JSON 对象在发送到 Node 服务器时发生更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41058694/