JavaScript到节点

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 ode_modules\express\libesponse.js:730:10)
at ServerResponse.send (C:\Users\Abhishek\Basics\first-express ode_modules\express\libesponse.js:170:12)
at done (C:\Users\Abhishek\Basics\first-express ode_modules\express\libesponse.js:967:10)
at Object.exports.renderFile (C:\Users\Abhishek\Basics\first-express ode_modules\jade\lib\index.js:374:12)
at View.exports.__express [as engine] (C:\Users\Abhishek\Basics\first-express ode_modules\jade\lib\index.js:417:11)
at View.render (C:\Users\Abhishek\Basics\first-express ode_modules\express\lib\view.js:128:8)
at tryRender (C:\Users\Abhishek\Basics\first-express ode_modules\express\lib\application.js:640:10)
at Function.render (C:\Users\Abhishek\Basics\first-express ode_modules\express\lib\application.js:592:3)
at ServerResponse.render (C:\Users\Abhishek\Basics\first-express ode_modules\express\libesponse.js:971:7)
at C:\Users\Abhishek\Basics\first-express\app.js:50:7 at Layer.handle_error (C:\Users\Abhishek\Basics\first-express ode_modules\express\libouter\layer.js:71:5)
at trim_prefix (C:\Users\Abhishek\Basics\first-express ode_modules\express\libouter\index.js:315:13)
at C:\Users\Abhishek\Basics\first-express ode_modules\express\libouter\index.js:284:7
at Function.process_params (C:\Users\Abhishek\Basics\first-express ode_modules\express\libouter\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;
});
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐