2017-09-06 18 views
0

データをデータベースに送信したい。私はポリマー+ nodeJSを使用します。 私は設定の多くを試みたが、私は常にエラーを持っている: POST 400(不正なリクエスト)POST 400(不正リクエスト)ポリマーアプリ

私は3つのファイルのアプリケーションheader.html、index.jsとsqlRequest.jsを使用しています。

アプリケーション-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の1バックエンドファイル

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"); 
     } 
    }); 
}; 
+0

node.jsのサーバーアドレスは? – Ofisora

+0

それはlocalhost:8080 – alexzerah

+0

あなたは完全なアドレスを置く必要がありますlocalhost:8080/getNotifBack – Ofisora

答えて

1

私は解決策を見つけました。それは鉄のajaxのためのボディ要素を欠いていた。

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