客户端将json对象接收到javascript

时间:2014-12-10 12:45:21

标签: javascript ajax json xmlhttprequest

我希望我的服务器将JSON对象发送到客户端的javascript。如何让客户端将对象放入他的javascript中,而不是在屏幕上显示对象?

在我的服务器中:

app.get('/', function (req, res) {
    res.send(jsonObj);
});

谢谢!

2 个答案:

答案 0 :(得分:2)

使用jquery我将向您展示一个简单的例子:

客户端

$.get('youserver.com/', {mydata: 'content'}, function(response){
   //callback triggered when server responds
   console.log(JSON.stringify(response));
});

服务器

app.get('/', function (req, res) {
  if(req.params.mydata === 'content'){
    res.end("you sent content");
  }  else {
    res.end("you sent something else");
  }
});

你明白我的意思吗?

答案 1 :(得分:0)

尝试使用res.json()方法

app.get('/', function (req, res) {
    res.json(jsonObj);
});