在服务器端,您将接收jQuery数据作为请求参数,而不是JSON。如果您以JSON格式发送数据,您将收到JSON并且需要对其进行解析。就像是:
$.ajax({ type: 'GET', url: 'door.validate', data: { jsondata: "{ "foo": "bar", "foo2": 3 }" // or jsondata: JSON.stringify(credentials) (newest browsers only) }, dataType: 'json', complete: function(validationResponse) { ... }});在服务器端,您将执行以下操作:
var url = require( "url" );var queryString = require( "querystring" );http.createServer( function (req, res) { // parses the request url var theUrl = url.parse( req.url ); // gets the query part of the URL and parses it creating an object var queryObj = queryString.parse( theUrl.query ); // queryObj will contain the data of the query as an object // and jsonData will be a property of it // so, using JSON.parse will parse the jsonData to create an object var obj = JSON.parse( queryObj.jsonData ); // as the object is created, the live below will print "bar" console.log( obj.foo ); }).listen(80);要将对象序列化为JSON并在jsonData中设置值,可以使用
JSON.stringify(credentials)(在最新的浏览器中)或JSON-js。



