Node.js 使用 AJAX 向后端发送数据 [英] Node.js send data to backend with AJAX

查看:53
本文介绍了Node.js 使用 AJAX 向后端发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 AJAX 还很陌生,很抱歉可能造成误解,但我还没有完全了解那件事.

I'm quite new to AJAX, so sorry for potential missunderstandings, but I'm not completely through that thing.

我正在尝试一件简单的事情.我有一个 server.js 文件,它基本上是我的后端.然后我有一个 index.html 和一个 script.js.就是这样,这是一个非常基本的设置.现在,在我的 script.js 上,我得到了一些数据(一个邮件地址).现在我想将该数据发送到我的后端(进入 server.js)以在那里使用它.我该怎么做?

I'm trying a simple thing. I have a server.js file, which is my backend basically. Then I have a index.html and a script.js. That's all, so a very basic setup. Now, on my script.js, I'm getting some data (a mail address). Now I want to send that data to my backend (into the server.js) to work with it there. How can I do this?

我已经发现一些关于 node.js 的 AJAX 的帖子,但我不明白,尤其是我的后端不知道在哪里接收它.顺便说一下,我在服务器上使用 express.

I found some posts already about AJAX with node.js, but I don't get it, especially not where to receive it in my backend. I'm using express for the server by the way.

我的 script.js 中有:

$.ajax({
            type: "POST",
            url: "server.js",
            data: { mail: mail },
            success: function(data) {
            },
            error: function(jqXHR, textStatus, err) {
                alert('text status '+textStatus+', err '+err)
            }
        });

到目前为止?我现在如何接收 server.js 中的信息?

Right so far? How can I now receive the information in my server.js?

目前没有太多内容,只是:

There's not much in so far, just:

var express = require('express');

var app = express();
var server = app.listen(3000);

app.use(express.static('public'));

感谢您的帮助:)

推荐答案

注意:这是在用代码更新问题之前写的,所以我在这里用作示例的字段名称和端口号可能需要更新了正确的值.

客户端代码 - jQuery 示例:

Client-side code - example with jQuery:

$.post('/email', { address: 'xxx@example.com' });

(这可以采用可选的回调,并返回一个可用于添加成功/错误处理程序的承诺)

(this can take optional callbacks and it returns a promise that can be used to add a success/error handler)

服务器端代码 - Express 示例:

Server-side code - example with Express:

const express = require('express');
const bodyParser = require('body-parser');

const dir = path.join(__dirname, 'public');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('/email', (req, res) => {
  // you have address available in req.body:
  console.log(req.body.address);
  // always send a response:
  res.json({ ok: true });
});

app.use(express.static(dir));

app.listen(4443, () => console.log('Listening on http://localhost:4443/'));

这假设您的静态文件(HTML、客户端 JavaScript、CSS)位于相对于您的 server.js 文件的 public 目录中.

This assumes that your static files (HTML, client-side JavaScript, CSS) are in the public directory relative to your server.js file.

有关 JSON/表单编码问题的背景信息,请参阅此内容:

See this for background on the JSON/form-encoding issue:

有关提供静态文件的背景信息,请参阅此内容:

See this for background on serving static files:

这篇关于Node.js 使用 AJAX 向后端发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆