简体   繁体   English

POST 400(错误请求)聚合物应用

[英]POST 400 (Bad request) polymer app

I want to send data to database. 我想将数据发送到数据库。 I use polymer + nodeJS. 我使用聚合物+ nodeJS。 i tried a lot of configuration but i always have an error: POST 400 (Bad Request) 我尝试了很多配置,但始终出现错误:POST 400(错误请求)

I use 3 files application-header.html, index.js and sqlRequest.js. 我使用3个文件application-header.html,index.js和sqlRequest.js。

application-header.html : Here is my front-end html file application-header.html:这是我的前端html文件

<iron-ajax 
    method="POST"
    id="getNotif"
    on-response="notificationAjax"
    content-type="application/json"
    handle-as="json">
</iron-ajax>

<div class="notification" on-click="notificationMenu">
    <span class="notificationSpan" id="notificationNumber"></span>
    <span class="disNone notificationMenuCss" id="notifBar"></span>
</div>

<script>
    Polymer({
        notificationMenu: function() {  
document.querySelector(".notificationMenuCss").classList.toggle("disNone");
            var notifNumber = 
document.getElementById('notificationNumber').innerHTML;
            notifNumber = this.DataObj;
            this.$.getNotif.url = "/getNotifBack";
            this.$.getNotif.generateRequest();
        },

        notificationAjax: function(e) {
            this.$.getNotif.url = "/getNotifBack";
            this.$.getNotif.generateRequest();
        }
    });

index.js : Here is one of my JS back-end file index.js:这是我的JS后端文件之一

var express = require('express');
var app = require('express')();
var http = require('http').Server(app);
var router = express.Router();
var logger = require('./winstonConf.js');

router.post('/getNotifBack', function (req, res, next) {
    var essai = require('./sqlRequest.js');
    essai.notification(req.body, function (index) {
        res.send(JSON.stringify(index));
    });
});

sqlRequest.js : Here is another of my JS back-end file sqlRequest.js:这是我的另一个JS后端文件

exports.notification = function (cb) {
    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'user',
        password: 'password',
        database: 'database'
    });

    var sqltable = "INSERT INTO notification VALUES (NULL, '1') ";
    connection.connect();
        connection.query(sqltable, function (err, results, fields) {
            if (err) {
                console.log('Error while performing Query');
                connection.end();
            } else {
            console.log('success performing Remove Query');
            connection.end();
            cb("success");
        }
    });
};

i found the solution. 我找到了解决方案。 It was missing a body element for iron ajax. 它缺少铁ajax的身体元素。

<iron-ajax 
    method="POST"
    id="getNotif"
    on-response="notificationAjax"
    content-type="application/json"
    handle-as="json">
    body = [{"element":"1"}]
</iron-ajax>

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

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