1

I have no way to fix the javascript. The page uses a 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 ?

I need to POST requests are redirected to the page to a different address.

the following code inserts the result in 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). Is there are any way to reload this web page from our server?

juppy
  • 21
  • 1
  • 6
  • Why not use jquery ajax? – Bon Macalindong Feb 10 '16 at 11:19
  • @BonMacalindong, the project uses pure js, but if used **jQuery** `function go_jQuery() { $.ajax("/home/someaction").done(function(count) { document.querySelector("h1").textContent = count; }); }` Page would not be redirected to another address, and the code of the page to come in `document.querySelector("h1").textContent` – juppy Feb 10 '16 at 12:41
  • The reason you are not redirected is because you're doing an ajax request even though you set on the controller the necessary response objects for redirection. You may check on this [SO post](http://stackoverflow.com/a/19795466/1504480) for the answer – Bon Macalindong Feb 10 '16 at 13:18
  • @BonMacalindong, if I understood correctly, in the example above, the decision was the ability to process the response from the server to the client, and depending on the answer has to produce `window.location.href = url` . But I do not get, because I can not change the JavaScript on the client side, it just does not contact the server for a new version of the script and the only way is to replace   `someaction` for the transition to the new site version. – juppy Feb 10 '16 at 13:37

1 Answers1

1

On your action method, you can call a Javascript code by returning a Javascript result

[HttpPost]
public ActionResult someaction() {
    if (Request.IsAjaxRequest()) {
        return JavaScript("document.location.href=\"http://www.google.com\"");
    } else {
        return Redirect("http://wwww.google.com");
    }
}
Community
  • 1
  • 1
Bon Macalindong
  • 1,310
  • 13
  • 20
  • I do not quite understand what you mean by that. return type is 'void' – juppy Feb 10 '16 at 15:12
  • even if you remove `return` redirection does not happen – juppy Feb 10 '16 at 15:26
  • Use ActionResult return type instead. Edited my post – Bon Macalindong Feb 11 '16 at 03:08
  • redirect to the new page does not work, instead of the new page is inserted into the code `document.querySelector("h1").textContent` – juppy Feb 11 '16 at 07:22
  • Have you tried debugging the action method? I assume it should go to the `if (Request.IsAjaxRequest())` part since you are calling an ajax request – Bon Macalindong Feb 11 '16 at 09:26
  • 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). Is there are any way to reload this web page from our server? – juppy Feb 11 '16 at 12:57