Friday, 28 April 2017

Post Request to NodeJS Backend

I am currently trying to post json data to a json file. In order to do so, I've setup a NodeJS and express server but something keeps messing up. I keep receiving this feedback:

jquery-3.2.1.js:9566 POST http://localhost:3000/Alpha/json/details.json 404 (Not Found)

I read several other questions stating that the issues is probably in my server side script and that I haven't specified the json file correctly.

JS Client Side Post script:

function sendFormData(){
            var formData = JSON.stringify($("#myForm").serializeArray());

            $.ajax({
                type: "POST",
                url:"../json/details.json",
                data: formData,
                success: function(){
                    alert("something!");
                }
            });
        } 

My HTML Form:

<form action="XXXXXXXX"id="myForm"><label for="id">ID</label>
    <input type="text" id="id" name="id" placeholder="Item ID number" ng-model="newDetails.id">

       <label for="system">System</label>
       <input type="text" id="system" name="lastname" placeholder="Type of System" ng-model="newDetails.system">

       <label for="date">Date</label>
       <input type="text" id="date" name="lastname" placeholder="dd-mm-yyyy"  ng-model="newDetails.date">

       <label for="priority">Priority</label>
       <select id="priority" name="priority" ng-model="newDetails.priority">
         <option value="high">High</option>
         <option value="medium">Medium</option>
         <option value="low">Low</option>
        </select>
        <label for="description">Description</label>
        <textarea class="form-control" rows="3" id="desc" name="description" ng-model="newDetails.description"></textarea>

        <label for="priority">Status</label>
        <select id="status" name="status" ng-model="newDetails.status">
          <option value="OPEN">OPEN</option>
          <option value="ON HOLD">ON HOLD</option
          <option value="CLOSED">CLOSED</option>
        </select>

       <input type="button" value="Submit" class="btn" data-dismiss="modal" style="background-color:#FFBE00; color:#002244;" onClick="sendFormData()">
</form>

NodeJS Script:

    var express = require('express');
    var app = express();
    var bodyParser = require('body-parser');
    var path = require('path');

    app.use(bodyParser.json());
    app.use('../json', express.static(path.join(__dirname, 'json')));
    app.use('/', express.static(__dirname + '/'));


    app.post('/formdata', function(req, res) {
        console.log( req.body.id + req.body.system + req.body.date + req.body.priority + req.body.description + req.body.status);
        res.send("ok");
    });

    app.use(bodyParser.json());

    // Get information submitted
    app.post('/', function(req, res) {
        console.log("Information received !");
        console.log(req.body);
        res.send("Ok!");
    });

app.listen(3000);

Is my Node Script working fine? Why cannot I find the json file?

root/Alpha/json/details.json - is the json file path

my node script is in the root directory



via User199932

No comments:

Post a Comment