【问题标题】:redirect to url taken from json response重定向到从 json 响应中获取的 url
【发布时间】:2013-12-04 00:53:40
【问题描述】:

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

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

window.location.href = url

当 url 干净且没有其他字符时它可以正常工作,但当我有 # 或空格或其他一些字符时会失败。请让我知道是否有任何方法可以解决我的问题。

【问题讨论】:

  • 给出一个失败的示例 URL...
  • 我在服务器端使用 json_encode 函数,所以我认为它正在编码特殊字符并创建类似 &abc;对于某些字符 [链接](example.com/… and_quotation_and_hash)

标签: javascript php jquery json redirect


【解决方案1】:

如果你使用 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");
    }
  }
 });
}

【讨论】:

    【解决方案2】:

    你试过 window.location = "url"

    网址应包含 http://

    它应该在引号内

    【讨论】:

      【解决方案3】:

      我个人使用

          window.location.replace(url);
      

      Read More - "replace() 方法将当前文档替换为新文档" window.location.replace() 更好地模拟了 http 重定向

      还有其他各种选项,例如:

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

      作为链接点击

      【讨论】:

      • window.location.replace(url);也没有用。我在服务器端使用 json_encode 函数,所以我认为它正在编码特殊字符,但我不知道为什么 window.location.replace(url) 不起作用,它至少应该尝试打开那个 url,它只是这样做什么都不做
      • 对不起,我认为我粘贴的内容就足够了。但我现在没有我的代码。今晚我将用我的源代码和一些失败的测试用例发表一篇新文章。
      • @jCloud 你可以在有机会的时候将代码添加到这篇文章中
      猜你喜欢
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 2016-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2013-02-24
      相关资源
      最近更新 更多