redirect/render after ajax post/get

6,887 views
Skip to first unread message

mircoz

unread,
Feb 27, 2012, 5:54:38 PM2/27/12
to Express
Hey,

I've got a weird problem. I'm using the following code to post data to
my express.js server:

$.ajax({
type: 'get',
url: '/searchapi',
data: {'street': street, city: city, 'streetnumber':
streetnumber},
});

My server code looks like this:

app.get('/searchapi', function(req, res) {
console.log(req.query.city);
console.log(req.query.street);
console.log(req.query.streetnumber);
res.redirect('/searchresults');
});

It all works fine except for the redirect. That does not work at all.
Same problem goes for the method/type "post" instead of "get".
I think the rendered page is somehow sent back to the jquery ajax
request and is processed there internally instead of rendering the new
page (in this case the search results from a database).

How can I render a new page from a jquery ajax get/post request?

Thanks guys!

Paul Vencill

unread,
Mar 1, 2012, 3:19:43 PM3/1/12
to expre...@googlegroups.com

That's exactly what is happening.  Your client success handler can set the window.location.href to the redirect url

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

jam...@gmail.com

unread,
Mar 2, 2012, 3:19:50 AM3/2/12
to expre...@googlegroups.com
Or assign the result to a div content

$.get(url, function (data) {
   $('#page-container').html(data);
});
//.error(function () {   }) 


2012/3/1 Paul Vencill <paul.v...@gmail.com>

z_mirco

unread,
Mar 14, 2012, 12:34:16 PM3/14/12
to expre...@googlegroups.com
ok thank you! I didn't really understand what was going on during the request. Using window.location.href is a good idea!
2012/3/1 Paul Vencill <paul.v...@gmail.com>
To unsubscribe from this group, send email to express-js+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/express-js?hl=en.

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+unsubscribe@googlegroups.com.

123tppd

unread,
Mar 14, 2012, 2:07:12 PM3/14/12
to expre...@googlegroups.com
The same problem, with different server response (render instead redirect)

$.ajax({
type: 'POST',
data: data,
url: '/criptografar',
success: function(data) {
console.log('success');
// $('body').html(data); <- i don't want it, but if not so, nothing happens (render)
}
});

app.get('/criptografar', function(req, res) { 
        console.log(req.something); 
        res.render('somepage', {somevar: withsomevalue}); <- I want this to work like a normal post
});

Thanks!
Reply all
Reply to author
Forward
0 new messages