redirect to url taken from json response

21,572

Solution 1

I personally use

    window.location.replace(url);

Read More - "The replace() method replaces the current document with a new one" window.location.replace() better simulates a http redirect

There are other various options like:

window.location.href = "http://stackoverflow.com";

Which acts as a link click

Solution 2

have u tried window.location = "url"

url should include http://

and it should be inside quotes

Solution 3

if you use jquery it would be easier. here's an example:

function save(){
var item = "value of some input";
 $.ajax({
  url      : "process.php",
  dataType : "json",
  data     : "item ="+item,
  type     : "POST",
  cache    : false,
  success : 
  function (data){
// if the return of your json like {"status":"success","url":"http url"}
// use this
    if (data.status == "success"){
      window.location.href = data.url;
    }
    else{
      alert("error occured");
    }
  }
 });
}
Share:
21,572
Jamol
Author by

Jamol

Hardworking, punctual and well-organized.

Updated on November 20, 2020

Comments

  • Jamol
    Jamol over 3 years

    I am using jquery ajax method to make http request to php webpage, and in response I am taking json like {"status":"success","url":"http url"}

    on success function I am redirecting to url from json but most of the time it fails. I am using following to redirect:

    window.location.href = url
    

    It works fine when url is clean with no other characters, but fails when I have # or space or some other characters. Please letme know if there is any way to solve my problem.