快速js - 不能重定向 [英] Express js - can't redirect

查看:80
本文介绍了快速js - 不能重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

I am trying to do the following:

来自客户端

var req =  jQuery.post(
  "http://www.example.com:3000"+"/dologin", 
  {"username" : username, "password" : password}).error(function(){
    alert("an error occurred");
  });

in express root

app.post('/dologin',function(req, res) {
  res.redirect('http://bbc.co.uk');
});

结果传回

<p>Moved Temporarily. Redirecting to <a href="http://bbc.co.uk">http://bbc.co.uk</a></p>

似乎如果我从jquery发布,重定向将无法正常工作。有没有人知道要强制重定向的方法?

Seems that if I do post from jquery the redirect will not work. Does anyone know a way to force it to redirect?

推荐答案

浏览器不会重定向ajax响应上的重定向窗口。

Browser does not redirect the window on redirect on ajax response. Redirect the browser with javascript.

在服务器中以新内容发送内容,例如。

In server send the new site as content, for example.

res.contentType('application/json');
var data = JSON.stringify('http://site.example.com/')
res.header('Content-Length', data.length);
res.end(data);

在客户端

var req =  jQuery.post(
   "http://www.mysite.com:3000"+"/dologin", 
    {"username" : username, "password" : password}, 'json').error(function(){
       alert("an error occurred");
    }).success(function(data) {
       window.location = data;
    });

这篇关于快速js - 不能重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆