javascript - 在 Express 服务器上获取从 AJAX 发送的数据

标签 javascript ajax express

下面的代码使用 AJAX 请求通过我的 Express 服务器发送数据。我需要在发送后在我的 express 服务器上访问该数据以进行一些操作。我该怎么做呢?

Express 服务器片段:

app.get("/time",function(req,res){
    client.messages.create({ 
        to: "+1" + req.data.number, 
        from: '+166**', 
        body: "This is the ship that made the Kessel Run in fourteen parsecs?", 
        mediaUrl: "https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg",  
    });
});

Ajax 请求:

$.ajax({url: "/time",
    data : { name : 'Justin', number : '662***' }
});

最佳答案

如果您正在谈论服务器中的 GET 变量:

app.get("/time",function(req,res){
    client.messages.create({ 
        to: "+1" + req.params.number,  // should be `req.params` instead
        from: '+166**', 
        body: "This is the ship that made the Kessel Run in fourteen parsecs?", 
        mediaUrl: "https://c1.staticflickr.com/3/2899/14341091933_1e92e62d12_b.jpg",  
    });
    console.log(req.params.name)
    console.log(req.params.number)
});

如果你说的是服务器的响应,那么:

$.ajax({
    url: "/time",
    data : { name : 'Justin', number : '662***' }
}).done(function (response) {
    console.log(response)
});

这个问题我看了三遍,还是没明白问什么,抱歉。

关于javascript - 在 Express 服务器上获取从 AJAX 发送的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41047771/

相关文章:

javascript - 在不中断 session 的情况下将 deathbycaptcha 与 phantomjs 一起使用

jquery - MVC/JQuery 输入通过 .val() 设置值

javascript - ExpressJs 中 AngularJs 范围内的 twitter API 变量

javascript - 正确使用 express.js API,如获取、发送、重定向

javascript - Protractor 不能使用以此声明的本地属性。 .then 语句内

javascript - 通过控制台关闭 Bootstrap 模式

asp.net - asp.net 中基于 Web 的聊天,使用 ajax 计时器,是否会为大量用户执行

javascript - 将未定义的 JSON 转换为新的 JSON 对象

javascript - Hello World - Backbone + Firebase + Backfire

javascript - 序列化js对象方法: is it really a problem