重定向到json响应中的url

redirect to url taken from json response

本文关键字:url 响应 json 重定向      更新时间:2023-09-26

我使用jquery ajax方法向php网页发出http请求,作为回应,我使用json,如{"status":"success","url":"http url"}

关于成功函数,我从json重定向到url,但大多数时候都失败了。我正在使用以下重定向:

window.location.href = url

当url是干净的,没有其他字符时,它工作得很好,但当我有#或空格或一些其他字符时会失败。如果有什么办法可以解决我的问题,请告诉我。

我个人使用

    window.location.replace(url);

阅读更多-"replace()方法用新文档替换当前文档"window.location.replacement()更好地模拟http重定向

还有其他各种选择,如:

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

作为链接点击

是否尝试过window.location="url"

url应包括http://

它应该在的引号内

如果使用jquery,会更容易。这里有一个例子:

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");
    }
  }
 });
}