【发布时间】:2015-02-10 04:52:14
【问题描述】:
在我的快速项目中,我希望我的服务器向客户端发送一个 JSON 对象。客户端使用脚本来显示它收到的数据,所以我不想直接在屏幕上看到从服务器发送的数据,我希望它首先通过 javascript。 我尝试了很多方法来发送任何数据,但都没有成功:
客户:
$.get('http://localhost:1337/', {mydata: 'content'}, function(response){
console.log(response);
});
或
$.ajax({
url: 'http://localhost:1337/',
data: {
mydata: "content"
},
dataType: 'json',
success: function (json) {
console.log(json);
},
error: function (jqXHR, error, errorThrown) {
console.log(error);
},
type: 'GET'
});
服务器:
app.get('/', function (req, res) {
res.render('main',{data:'text'})
});
或
app.get('/', function (req, res) {
res.render('main','text2')
});
有没有一种方法可以将数据发送到首先发送到发送页面中的 javascript 的客户端? 谢谢!
【问题讨论】:
标签: javascript node.js express