我正在使用jquery ajax方法向php网页发出http请求,作为回应我正在使用json像{“status”:“success”,“url”:“http url”}
关于成功函数我从json重定向到url,但大部分时间都失败了。我正在使用以下重定向:
window.location.href = url
当url干净而没有其他字符时它工作正常,但是当我有#或空格或其他字符时失败。请知道是否有办法解决我的问题。
答案 0 :(得分:6)
我个人使用
window.location.replace(url);
Read More - “ replace()方法用新的替换当前文档 window.location.replace()更好地模拟http重定向
还有其他各种选项,如:
window.location.href = "http://stackoverflow.com";
用作链接点击
答案 1 :(得分:2)
你试过window.location =“url”
网址应包含http://
它应该在引号内
答案 2 :(得分:2)
如果你使用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");
}
}
});
}