将JSON对象从客户端JavaScript传递到NodeJS
我有一个基于用户输入创建JSON对象的网页。然后,我希望以某种方式允许用户将此JSON对象提交给NodeJS脚本,以便处理/插入MySQL数据库。然而,我真的不知道如何做到这一点-我能想出的最好的办法是某种形式的帖子,但我不知道从哪里开始 因为我不知道这样一种方法会被描述成什么,所以我没有在网上找到任何教程或其他资源 有人能推荐一些与此相关的文章或文档吗?或者,至少,告诉我该搜索什么?谢谢 编辑:这是我目前正在尝试使用的代码。我只是尝试在两侧将POST数据类型从字符串转换为JSON 服务器端:将JSON对象从客户端JavaScript传递到NodeJS,javascript,json,node.js,Javascript,Json,Node.js,我有一个基于用户输入创建JSON对象的网页。然后,我希望以某种方式允许用户将此JSON对象提交给NodeJS脚本,以便处理/插入MySQL数据库。然而,我真的不知道如何做到这一点-我能想出的最好的办法是某种形式的帖子,但我不知道从哪里开始 因为我不知道这样一种方法会被描述成什么,所以我没有在网上找到任何教程或其他资源 有人能推荐一些与此相关的文章或文档吗?或者,至少,告诉我该搜索什么?谢谢 编辑:这是我目前正在尝试使用的代码。我只是尝试在两侧将POST数据类型从字符串转换为JSON 服务器端:
var express = require('express');
var fs = require('fs');
var app = express();
app.use(express.bodyParser());
app.get('/', function(req, res){
console.log('GET /')
//var html = '<html><body><form method="post" action="http://localhost:3000">Name: <input type="text" name="name" /><input type="submit" value="Submit" /></form></body>';
var html = fs.readFileSync('index.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
});
app.post('/', function(req, res){
console.log('POST /');
console.dir(req.body);
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('thanks');
});
port = 8080;
app.listen(port);
console.log('Listening at http://localhost:' + port)
客户端:
<html>
<body>
<form method="post" action="http://localhost:8080">
Name: <input type="text" name="name" />
<input type="submit" value="Submit" />
</form>
<script type="text/JavaScript">
console.log('begin');
var http = new XMLHttpRequest();
var params = "text=stuff";
http.open("POST", "http://localhost:8080", true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//http.setRequestHeader("Content-length", params.length);
//http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
console.log('onreadystatechange');
if (http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
else {
console.log('readyState=' + http.readyState + ', status: ' + http.status);
}
}
console.log('sending...')
http.send(params);
console.log('end');
</script>
</body>
</html>
下面是一个使用jQuery执行post请求和express应用程序的非常基本的示例。我认为这应该是一个不错的起点
// client side, passing data to the server
$.post("/foo/", { data : { foo : "bar" } }, function(temp) {
// temp === "I am done";
});
// serverside app.js
var express = require("express");
var app = express();
// will parse incoming JSON data and convert it into an object literal for you
app.use(express.json());
app.use(express.urlencoded());
app.post("/foo/", function(req, res) {
// each key in req.body will match the keys in the data object that you passed in
var myObject = req.body.data;
// myObject.foo === "bar"
res.send("I am done");
});
编辑:JSON.stringify和JSON.parse将序列化/反序列化JSON。jQuery使这变得更容易,但如果您想使用纯javascript
更改为var params=text=+JSON.stringify{foo:bar}
及
console.dirJSON.parsereq.body.text
它在我的本地网站上对我有效。一篇帖子是正确的,如果你想在不刷新页面的情况下实现这一点,你需要AJAX。您还需要一个在Express中的服务器上捕获此消息的路由,例如app.post/api/test谢谢您的示例代码。不幸的是,我对Express和Node.js还比较陌生,所以我无法让它运行。你能用这个给我指一下正确的方向吗?目前,我只创建了一个按钮,它应该触发post操作,如下所示:$.postlocalhost:8080/hello,{data:{foo:bar}},functiontemp{alerttemp;哪个部分不起作用,你可以用你的express应用程序的代码编辑你的问题。老实说,我不确定-我已经添加了上面的代码。谢谢你的帮助!我已经成功地获得了一个字符串,但现在的问题是将其更改为JSON。有什么想法吗?