简体   繁体   English

重定向到新页面以响应XHR发布请求

[英]Redirect to new page as response to XHR post request

I have no way to fix the javascript. 我没有办法修复JavaScript。 The page uses a XHR 该页面使用XHR

function openPOST(url, params, callback) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", url, true);
        xmlhttp.setRequestHeader('Content-Type', "application/x-www-form-rlencoded");
        xmlhttp.send(params);
        xmlhttp.onreadystatechange = function() {
            if (this.readyState === 4) {
                if (xmlhttp.status === 200) {
                    if (callback) callback(this.responseText);
                }
            }
        }
        return xmlhttp;
    };

At the time of page load, use the following query 在页面加载时,使用以下查询

document.addEventListener('DOMContentLoaded', function () {
        ...
        function go() {
            openPOST("/home/someaction", "query=123", function (count) {
                document.querySelector("h1").textContent = count;
            });
        }
        ...
        go();
        ...
});

Is it possible to implement such a code to ASP.NET MVC so he moved on to another page, but did not put the result in another page ? 是否可以在ASP.NET MVC中实现这样的代码,以便他转到另一页,但没有将结果放在另一页中?

I need to POST requests are redirected to the page to a different address. 我需要将POST请求重定向到页面的其他地址。

the following code inserts the result in document.querySelector("h1").textContent 以下代码将结果插入document.querySelector("h1").textContent

public void someaction() {
            Response.Status = "307 Temporary Redirect";
            Response.AddHeader("Location", "http://example.com");
        }

EDIT: User opened an old version of our webpage in his browser. 编辑:用户在浏览器中打开了我们网页的旧版本。 This webpage makes an ajax call to our webserver and inserts response as a string (no eval(), js code won't work). 该网页对我们的网络服务器进行了ajax调用,并将响应作为字符串插入(没有eval(),js代码无效)。 Is there are any way to reload this web page from our server? 有什么办法可以从我们的服务器上重新加载该网页?

On your action method, you can call a Javascript code by returning a Javascript result 在您的操作方法上,您可以通过返回Javascript结果来调用Javascript代码

[HttpPost]
public ActionResult someaction() {
    if (Request.IsAjaxRequest()) {
        return JavaScript("document.location.href=\"http://www.google.com\"");
    } else {
        return Redirect("http://wwww.google.com");
    }
}

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

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