我没有办法修复JavaScript。该页面使用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;
    };


在页面加载时,使用以下查询

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


是否可以在ASP.NET MVC中实现这样的代码,以便他转到另一页,但没有将结果放在另一页中?

我需要将POST请求重定向到页面的其他地址。

以下代码将结果插入document.querySelector("h1").textContent

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


编辑:
用户在浏览器中打开了我们网页的旧版本。该网页对我们的网络服务器进行了ajax调用,并将响应作为字符串插入(没有eval(),js代码将不起作用)。有什么办法可以从我们的服务器重新加载该网页?

最佳答案

在您的操作方法上,您可以通过返回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");
    }
}

关于javascript - 重定向到新页面以响应XHR发布请求,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35311245/

10-12 12:56
查看更多