简体   繁体   中英

Javascript redirect not working ASP.NET

I am currently creating a website. The issue I am stuck is I cannot redirect to another ASPX page using Javascript. Here goes my code:

<asp:Button ID="Save" runat="server" Text="Schedule" CssClass="vs-btn mT10" OnClientClick="return SendFormValuesForXml(); return false;" />

The above Javascript function performs an AJAX call and upon success I redirect to another page where several things are being performed. AJAX call goes here:

var responseString = "";

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Default.aspx/MakeXml",
    dataType: "json",
    data: "{'data':'" + stringToSend + "'}",
    async: false,
    cache: false,
    success: function (result) {
        debugger;
        window.location.reload(true);
        var valuationUrl = window.location.protocol + "//" + window.location.host + "/ValuationList.aspx";
        window.location.assign(valuationUrl);
        return false;
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Status: " + textStatus); alert("Error: " + errorThrown);
    }
});

Now everything is happening correctly, except the redirection in the success function. Any idea what is interfering with the javascript? Additional information:

  1. No update panel is working on the page, or any other page of the website.
  2. No error is thrown by the browser except the bad Chrome 'jquery.min.map' not found error.

What I have so far tried:

  1. document.location.replace(valuationUrl);
  2. location.href = valuationUrl;
  3. $(location).attr('href', valuationUrl);

And all other options that are mentioned on stack overflow's this link .

Update: Bad Solution I adopted After doing a lot of things, like taking a non-visible button and running the server side function on its Click to redirect page and other things, all of them failed. What I did is I set a hidden field with 0, and then update its value to 1. Later when the same page loads, I checks if its value is 1, so I force redirection to the next page.

Try window.location = valuationUrl; -- if it works, then you are running into a security violation due to the domain you're redirecting to being different from the one redirecting from.

Mozilla Developer Network Location.Assign

The Location.assign() method causes the window to load and display the document at the URL specified.

If the assignment can't happen because of a security violation, a DOMException of the SECURITY_ERROR type is thrown. This happens if the origin of the script calling the method is different from the origin of the page originally described by the Location object, mostly when the script is hosted on a different domain.

Mozilla Developer Network Location.Replace

The Location.replace() method replaces the current resource with the one at the provided URL. The difference from the assign() method is that after using replace() the current page will not be saved in session History, meaning the user won't be able to use the back button to navigate to it.

If the assignment can't happen because of a security violation, a DOMException of the SECURITY_ERROR type is thrown. This happens if the origin of the script calling the method is different from the origin of the page originally described by the Location object, mostly when the script is hosted on a different domain.

window.location.reload(true) causes the page to reload from the server. I'm pretty sure that means it will not continue executing the rest of that function.

Try this.

    success: function (result) {
        var valuationUrl = window.location.protocol + "//" + window.location.host + "/ValuationList.aspx";
        window.location.href=valuationUrl;
        return false;
    },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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