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

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

问题描述

对于AJAX来说,我很新,对于潜在的误会很抱歉,但我并不完全是这样的。



我正在尝试一个简单的事情。我有一个 server.js 文件,这是我的后端基本上。然后我有一个 index.html 和一个 script.js 。这就是一个非常基本的设置。现在,在我的 script.js 上,我收到一些数据(邮件地址)。现在我想将这些数据发送到我的后端(进入 server.js )在那里工作。我如何做到这一点?



我发现有一些关于AJAX的帖子是node.js,但是我没有看到,特别是在后台收到它的地方。我在使用快递服务器的方式。



我在我的 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中收到这些信息?



目前还没有太多,只是:

  var express = require('express'); 

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

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

感谢任何帮助:)

解决方案

注意:这是在使用代码更新问题之前编写的,因此我用这里作为示例的字段名称和端口号可能需要使用正确的值进行更新。 / em>



客户端代码 - 使用jQuery的示例:

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

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



服务器端代码 - 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)=> {
//你有地址可用在req.body:
console.log(req
//总是发送一个响应:
res.json({ok:true});
});

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

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

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



这是JSON /表单编码问题的背景:





有关提供静态文件的背景,请参阅:




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

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?

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.

What I have in my script.js is:

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

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'));

Thanks for any help :)

解决方案

Note: This was written before the question was updated with the code so the field names and port numbers that I used here as examples may need to be updated with the correct values.

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)

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/'));

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

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

See this for background on serving static files:

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

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