2

I am currently using a form builder (https://github.com/kevinchappell/formBuilder) which provides me with a GUI to build custom forms. The output I get is in JSON form, stored in a JavaScript variable. I need to use this JavaScript variable in the back end to store in my MongoDB.

I tried using AJAX for this purpose however I failed to retrieve the value. My AJAX code is as follows:

document.getElementById('getJSON').addEventListener('click', function() {
  var ans = formBuilder.actions.getData('json', true);


    $.ajax({
        type: "POST",
        data: ans,
        url: "/j",
        success: function(data){
            console.log(data);
        }
    }); 
   document.forms["myForm"].submit();

And at the server end:

router.post('/j', function(req, res, next) {
    req.session.fdata = req.body;
    res.redirect('/log2');
});

I keep getting this error:

Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:504:11)
at ServerResponse.setHeader (_http_outgoing.js:511:3)
at ServerResponse.header (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\response.js:730:10)
at ServerResponse.send (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\response.js:170:12)
at done (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\response.js:967:10)
at Object.exports.renderFile (C:\Users\Abhishek\Basics\first-express\node_modules\jade\lib\index.js:374:12)
at View.exports.__express [as engine] (C:\Users\Abhishek\Basics\first-express\node_modules\jade\lib\index.js:417:11)
at View.render (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\view.js:128:8)
at tryRender (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\response.js:971:7)
at C:\Users\Abhishek\Basics\first-express\app.js:50:7 at Layer.handle_error (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\router\index.js:315:13)
at C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Abhishek\Basics\first-express\node_modules\express\lib\router\index.js:335:12)

My /log2 route is

router.get('/log2', function(req, res, next) {
  res.render('log2',{ fdata: req.session.fdata});
  res.session.fdata=null;
});
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
abhimalik
  • 131
  • 1
  • 1
  • 7
  • 1
    what do you have in '/log2' route? "Can't set headers after they are sent" error occurs when response is sent more than once from the nodejs server – L4reds Jul 17 '17 at 09:13
  • Might be worth taking at look at this answer: https://stackoverflow.com/questions/7042340/error-cant-set-headers-after-they-are-sent-to-the-client – T Vernon Jul 17 '17 at 09:17
  • @TVernon thanks for the reply, however I was not able to understand the provided solution on that post – abhimalik Jul 17 '17 at 09:25
  • @L4reds hey, I added my log2 route too – abhimalik Jul 17 '17 at 09:26
  • router.get('/log2', function(req, res, next) { return res.render('log2',{ fdata: req.session.fdata}); }); try this and see if it works. If it works, then maybe, we can do something about resetting request session fdata. – L4reds Jul 17 '17 at 11:26
  • @L4reds That eliminated the error, thanks a million. But I failed to understand how, also while I was trying to resolve this i noticed that if I do not redirect to /log2 and instead just printed the AJAX passed variable on the console it did so with no error – abhimalik Jul 17 '17 at 11:49
  • also while i was trying to eliminate this error, I could successfully print the AJAX passed variable to the log but not redirect to log2 – abhimalik Jul 17 '17 at 12:06
  • I have finally gotten it to work, @L4reds thank you so much for your help. Turns out the inclusion of the return keyword solved my error. I shall try to deduce how exactly that worked. Thanks again! – abhimalik Jul 17 '17 at 13:09

0 Answers0