简体   繁体   English

从Ajax脚本重定向的Javascript页面

[英]Javascript page redirect from Ajax script

Due to some unforeseen reason, I cannot use a *.php extension for my page. 由于一些无法预料的原因,我不能在页面上使用* .php扩展名。 And I need to put conditional redirection in the page. 我需要在页面中放置条件重定向。

I plan to use *.html, and javascript/Ajax to call a PHP which evaluates the condition and accordingly sends back a redirection command. 我计划使用* .html和javascript / Ajax来调用PHP来评估条件并相应地发回一个重定向命令。

The condition is being evaluated, but how can I give a redirection command from this internal PHP is not clear to me. 正在评估这个条件,但是我怎么能从这个内部PHP中提供重定向命令对我来说并不清楚。

You can actually map all your .html files to .php using .htaccess. 您实际上可以使用.htaccess将所有.html文件映射到.php。

If that doesn't work for you, your logic should be as follows: 如果这对您不起作用,您的逻辑应该如下:

PHP: PHP:

echo json_encode(array("redirect_url" => $url));

Javascript with jQuery (assuming you require AJAX to redirect): 使用jQuery的Javascript(假设您需要AJAX重定向):

$.getJSON("PHP/Controller/Url", function(data) {
    window.location.href = data.redirect_url;
});

Javascript (if you simply serve a static html file) Javascript(如果您只是提供静态html文件)

<script>
window.location.href = "redirect_url";
</script>

You can use 您可以使用

window.location = "http://new-url"

or 要么

window.location.href = "http://new-url"

In JS, 在JS中,

location.assign("http://www.google.com")

will redirect your current frame to Google. 将您当前的框架重定向到Google。

So don't send redirect header in PHP, send "flags" in response body and use JS to redirect. 所以不要在PHP中发送重定向头,在响应体中发送“flags”并使用JS重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM