0

I created a UI to read, write and update a config file using HTML but I am not able to save new JSON object to my existing config file.

So I need to post JSON object from HTML file to nodejs server

JAVASCRIPT

 var testJson = { name: "mufashid" }
        $.ajax({
            url: 'http://localhost:3000/test/config',
            type: 'POST',
            dataType: 'json',
            data: { o: JSON.stringify(testJson ) }, // bags collection value what your goging to send to server 
            // crossDomain: true,
            success: function (data) {
                alert('Success!')
            },
            error: function (jqXHR, textStatus, err) {
                //show error message
                alert('text status ' + textStatus + ', err ' + err)
            }
        });

nodejs


app.post('/test/config', function (req, res) {
    // console.log(res);
    console.log(req)

})

Need to update the config file with the new value when pressed the save button.

9
  • Have you tried req.data? You can try using console.log to debug Commented Aug 6, 2019 at 7:42
  • @Shinjo Yes i have tried req.data it showing undefined. Commented Aug 6, 2019 at 7:44
  • what does console.log(req) return? Commented Aug 6, 2019 at 7:45
  • Did you add app.use(require('express').json())? Commented Aug 6, 2019 at 7:48
  • 1
    What about your (res)? Could help: stackoverflow.com/questions/45105992/…, stackoverflow.com/questions/13478464/…. Once you're able to access your json from ajax request you could easily manipulate the data. Commented Aug 6, 2019 at 7:56

2 Answers 2

-1

You missed contentType: "application/json" in your javascript code.

var testJson = { name: "mufashid" }
        $.ajax({
            url: 'http://localhost:3000/test/config',
            type: 'POST',
            contentType: "application/json",// you missed this line.
            dataType: 'json',
            data: JSON.stringify({ o: testJson }), // IMportant!!!!!!
            // crossDomain: true,
            success: function (data) {
                alert('Success!')
            },
            error: function (jqXHR, textStatus, err) {
                //show error message
                alert('text status ' + textStatus + ', err ' + err)
            }
        });

Sign up to request clarification or add additional context in comments.

Comments

-1

Please find the below code. Content type was missing

$.ajax({
    url: 'http://localhost:3000/test/config',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json',
    data: JSON.stringify({
      o: testJson
    }),
    // crossDomain: true,
    success: function (data) {
      alert('Success!')
    },
    error: function (jqXHR, textStatus, err) {
      //show error message
      alert('text status ' + textStatus + ', err ' + err)
    }
  });

Routes

app.post('/test/config', function (req, res) {
  console.log(req.body.o);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.