簡體   English   中英

POST 400(錯誤請求)聚合物應用

[英]POST 400 (Bad request) polymer app

我想將數據發送到數據庫。 我使用聚合物+ nodeJS。 我嘗試了很多配置,但始終出現錯誤:POST 400(錯誤請求)

我使用3個文件application-header.html,index.js和sqlRequest.js。

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:這是我的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:這是我的另一個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");
        }
    });
};

我找到了解決方案。 它缺少鐵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