【问题标题】:Send/receive data from Javascript to Node.js using Express使用 Express 从 Javascript 向 Node.js 发送/接收数据
【发布时间】:2016-08-05 12:27:45
【问题描述】:

我目前正在使用 Express 平台、Twilio Node.js SMS API 和显然是 javascript 来向我的用户发送文本消息。问题是,我不知道应该怎么做才能通过前端的 GET 变量发送数据并在后端使用 node.js 捕获这些值。

出于测试目的,我创建了一个简单的按钮,当点击该按钮时,它会向固定号码发送一条短信。

这里是 javascript 方面:

function sms() {
   xmlhttp = new XMLHttpRequest();
   xmlhttp.open("GET","http://localhost:5001", true);
   xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
           alert(xmlhttp.responseText);
        } 
   }
   xmlhttp.send();
}

这里是 node.js 端:

var accountSid = 'ACCOUNT_SID';
var authToken = 'ACCOUNT_TOKEN';
//require the Twilio module and create a REST client
var client = require('twilio')(accountSid, authToken); 

var express = require("express");
var app = express();

app.get('/',function(request,response){
    
    var to = "TO";
    var from = "FROM";
    client.messages.create({
        to: to,
        from: from,
        body: 'Another message from Twilio!',
    }, function (err, message) {
        console.log("message sent");
        
    }); 

});
app.listen(5001); 

我遇到了两种从 Node.js 发送 responseText 的方法,但无法让它们工作 第一个使用response.send("Hello World"); 或第二个response.write("Hello again"); response.end();

总结一下,我想通过我的http请求发送变量(to、from、message等),在node.js中捕获它们并发送一个responseText!作为提醒,我对 JS 和 PHP 之间的 AJAX 请求非常满意,但 Node.js 对我来说是新的。

提前致谢

【问题讨论】:

  • 您提供的两段代码都应该可以工作,但您没有在服务器端代码中显示它们。有什么问题?您将它们放在服务器端代码的什么位置?当你提出请求时会发生什么?您是否使用浏览器中的开发人员工具来检查请求?它会得到 200 OK 响应吗?它是否包含您期望的数据?您是否在控制台中查看过错误消息? (我希望看到一个跨源异常,这很容易在 Google 中找到解决方案)
  • 一个小提示,你不会向前端发送任何东西来通知它成功。为此,一个简单的 response.send() 或 response.send('Received the data');会告诉前端 200。
  • @Quentin 当我在浏览器中发出请求时,我确实得到了 200 OK 响应,但是当我在 JS 中评估它时,我无法提醒 responseText。控制台日志中没有令人担忧的错误消息。
  • @Brant 我将你所说的添加到我的个人代码中,但我无法使用 xmlhttp.responseText 在 JS 端提醒它?怎么会?
  • 您在浏览器的开发人员工具中查看控制台?不是 Node.JS 控制台?当您通过 XMLHttpRequest 加载 URL 时,您正在做什么?

标签: javascript node.js express get httpresponse


【解决方案1】:

我认为新指南将帮助您了解如何在 Node.js 中接收和回复短信:

https://www.twilio.com/docs/guides/sms/how-to-receive-and-reply-in-node-js

右侧的 CodeRail 将逐步引导您完成,但您应特别注意标题为“生成动态 TwiML 消息”的部分。

var http = require('http'),
    express = require('express'),
    twilio = require('twilio'),
    bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({ extended: true })); 

app.post('/', function(req, res) {
    var twilio = require('twilio');
    var twiml = new twilio.TwimlResponse();
    if (req.body.Body == 'hello') {
        twiml.message('Hi!');
    } else if(req.body.Body == 'bye') {
        twiml.message('Goodbye');
    } else {
        twiml.message('No Body param match, Twilio sends this in the request to your server.');
    }
    res.writeHead(200, {'Content-Type': 'text/xml'});
    res.end(twiml.toString());
});

http.createServer(app).listen(1337, function () {
    console.log("Express server listening on port 1337");
});

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 2020-04-10
    • 2014-11-02
    相关资源
    最近更新 更多