gpt4 book ai didi

javascript - POST 400(错误请求) polymer 应用程序

转载 作者:行者123 更新时间:2023-12-03 03:31:26 26 4
gpt4 key购买 nike

我想将数据发送到数据库。我用的是polymer+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 的 body 元素。

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

关于javascript - POST 400(错误请求) polymer 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46078825/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com