我在node.js中创建了一个模块。基本上,此模块从mongodb获取数据并返回JSON值。
现在在Express.js中,前端开发人员(不是我)正在使用jQuery插件来执行操作。此jQuery需要使用JSON变量作为参数来完成其任务。
现在,我不知道如何将JSON变量(在服务器端生成)发送到在客户端工作的jQuery插件。我无法全神贯注于如何开始执行此操作,因此我很难进行搜索。不知道关键字是什么。因此,无论是技术答复还是对这项工作的一般解释都将受到赞赏。
尝试以下代码:
客户端jQuery:
$.ajax({
type: 'POST',
url: 'http://localhost:3000/request',
data: {
test: "test"
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success : function(result){
alert(result);
},
error : function(){
console.log("error")
}
});
在Node.js中,
尝试以下服务器端代码来处理/request
:
app.get('/request', function(req, res){
var data = {'TestKey':'TestValue'};
//For test at server side only
console.log('Sent this data to client:\n' + JSON.stringify(data));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(data));
});
res.writeHead..
& res.end..
res.json(data);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句