使用window.location.replace()重定向AJAX成功

时间:2014-09-01 14:05:08

标签: javascript ajax coffeescript

我正在尝试在成功的ajax调用上重定向页面,以下代码工作正常,

$.ajax(
      {
      type: "POST",
      url: path,
      data: response1,
      contentType: "application/json",
      success: ->

                 window.location.replace("http://172.16.17.141:81/configuration/manage_users")

      });

这种方法的问题是我给出的路径是固定的,我想要类似的东西,

$.ajax(
      {
      type: "POST",
      url: path,
      data: response1,
      contentType: "application/json",
      success: ->
                 alert(window.location.host + "/configuration/manage_users")#Gives right path

                 window.location.replace(window.location.host + "/configuration/manage_users")   
                 #Does not work, I even tried window.location.host.toString()
      });

页面不会使用上述方法重定向,而是页面重定向到" about:blank"。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

问题在于我没有指定协议,

window.location.replace(window.location.protocol + "//" +
                                  window.location.host + "/configuration/manage_users")

工作正常,我发现了,

window.location = window.location.protocol + "//" +
                                         window.location.host + "/configuration/manage_users"

更适合重定向。

答案 1 :(得分:0)

通常你可以从服务器上做到这一点。您必须回复正确的HTTP标头。你的后端技术是什么?在node.js中,它将如下所示:

var host = 'http://172.16.17.141:81'
response.writeHead(302, {
  'Location': host + '/configuration/manage_users'
});
response.end();

答案 2 :(得分:0)

这会有用吗?

$.ajax
  type: 'POST'
  url: path
  data: response1
  contentType: 'application/json'
  success: ->
    window.location = "//#{window.location.host}/configuration/manage_users"
相关问题