简体   繁体   English

使用Node,Express和Vanilla JS进行POST Ajax调用

[英]POST Ajax calls with Node, express & vanilla JS

I am trying to do some ajax calls with vanilla JS. 我试图用香草JS做一些Ajax调用。 In the back-end I am working with node in the express-framework . 在后端,我正在快速框架中使用node。

It seems that the data I want to send never actually reaches my back-end. 似乎我要发送的数据实际上从未到达后端。 Has anyone an idea what is going on? 有人知道发生了什么吗?

My Javascript : 我的Java语言

<form id='ajax_form' method='POST' action='/admin/new/tag'>
        <input type='text' name='tag' id="tag">
        <button type='submit'>Create new tag</button>
</form>

<script>
    var ajaxForm = document.querySelector("#ajax_form").addEventListener("submit", function(e){
                    e.preventDefault();
                    var form = e.target;
                    var data = new FormData(form);
                    var request = new XMLHttpRequest();
                    request.onreadystatechange = function(){
                        if(request.response){
                        [...]
                        }
                    }
                    request.open(form.method, form.action)
                    request.send(data);
            });
</script>

When I iterate over the data object, everything seems to be as it should, returning the keys and values I want to submit through the form. 当我遍历数据对象时,一切似乎都应该正确,并返回了我想通过表单提交的键和值。

This is my back-end set-up: 这是我的后端设置:

var express = require("express"),
    app = express(),
    mysql = require("mysql"),
    bodyParser = require("body-parser");

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


app.post('/admin/new/tag', function(req, res){
    var tag = {tag_name: req.body.tag};
    var q = 'INSERT INTO tags SET ?';
    connection.query(q, tag, function(error, results, fields){
        if (error) throw error;
        res.send('succeed');
    });
});

When I console.log(req.body.tag) , I just get Undefined and req.body is just an empty object. 当我console.log(req.body.tag) ,我只是得到Undefined并且req.body只是一个空对象。 My database also throws an error as the key tag_name should not be NULL. 我的数据库也引发了错误,因为键tag_name不应该为NULL。

When I look at the network panel I receive 503 service unavailable . 当我查看网络面板时,收到503服务不可用的消息

Thank you for your response and input! 感谢您的回复和投入!

On server-side you muse use multer for handling multipart/form-data. 在服务器端,您必须使用multer处理多部分/表单数据。

Alternatively, you can send the data in JSON format to the server like this way: 另外,您可以通过以下方式将JSON格式的数据发送到服务器:

var xhr = new XMLHttpRequest();
var data = {
    param1: 'value1',
    param2: 'value2'
};
xhr.open('POST', '/query');
xhr.onload = function(data) {
    console.log('loaded', this.responseText);
};
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));

Server-side set the bodyParser to JSON 服务器端将bodyParser设置为JSON

app.use( bodyParser.json() );

And now you should be able to read the values ​​through the req.body.. syntax. 现在您应该能够通过req.body..语法读取值了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM