【问题标题】:Redirection on AJAX success using window.location.replace()使用 window.location.replace() 重定向 AJAX 成功
【发布时间】:2014-10-25 18:51:16
【问题描述】:

我正在尝试在成功的 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”而不是 URL。

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript ajax coffeescript


    【解决方案1】:

    这行得通吗?

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

    【讨论】:

      【解决方案2】:

      问题是我没有指定协议,

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

      工作正常,我发现了,

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

      更适合重定向。

      【讨论】:

        【解决方案3】:

        通常您可以从服务器执行此操作。您必须响应正确的 HTTP 标头。你的后端技术是什么?在 node.js 中它看起来像这样:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-06
          • 1970-01-01
          • 1970-01-01
          • 2015-07-12
          • 2023-03-20
          • 1970-01-01
          • 1970-01-01
          • 2020-05-18
          相关资源
          最近更新 更多