问题
I am trying to redirect the page on the successful ajax call, the following code works fine,
$.ajax(
{
type: "POST",
url: path,
data: response1,
contentType: "application/json",
success: ->
window.location.replace("http://172.16.17.141:81/configuration/manage_users")
});
Problem with this approach is that the path I am giving is fixed, I want something like,
$.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()
});
The page does not redirect with the above approach and instead of the URL, the page redirects to "about:blank".
Any help will be appreciated.
回答1:
The problem was that I was not specifying the protocol,
window.location.replace(window.location.protocol + "//" +
window.location.host + "/configuration/manage_users")
worked fine, I found out that,
window.location = window.location.protocol + "//" +
window.location.host + "/configuration/manage_users"
is better for redirection.
回答2:
Normally you could do this from server. You have to respond correct HTTP headers. What is your back end technology? In node.js it will look like this:
var host = 'http://172.16.17.141:81'
response.writeHead(302, {
'Location': host + '/configuration/manage_users'
});
response.end();
回答3:
Would this work?
$.ajax
type: 'POST'
url: path
data: response1
contentType: 'application/json'
success: ->
window.location = "//#{window.location.host}/configuration/manage_users"
来源:https://stackoverflow.com/questions/25607582/redirection-on-ajax-success-using-window-location-replace