Saturday 27 May 2017

ReferenceError: window is not defined after Ajax call trying to redirect the user

I am new to express with EJS.

I tried making a ajax post request for submitting to the values to the DB using.

 $.ajax({
    type: 'POST',
    url: '/register',
    data: user,
    success: function(data){
        console.log(data);
        return data;
    }
  });

  return false;

This call is successful and after which the control goes to the calling function

var sess;

  app.post('/register',urlencodedParser,function(req,res){
    sess=req.session;
    var newUsr=Users(req.body).save(function(err,data){
      if(err) throw err;
      console.log(" back from ajax with data :::; " + data);

      console.log("step1 cleared");
      sess.registered=true;

    });
    console.log("step2 cleared");
    location.window.href = '/index2';
  });

I want the user to see the index page once the registration is successful, but I am getting the below error.

ReferenceError: location is not defined

I think location object is not there in node, thus this error, but again I dont know how to fix this issue, please suggest



via Saurabh Jhunjhunwala

No comments:

Post a Comment