0

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.

8
  • 1
    You can just use window.location.href = "/valuationlist.aspx". You don't need to do any of that protocol stuff. Commented Sep 3, 2014 at 16:51
  • No this did not work. Should I remove the reload()? Commented Sep 3, 2014 at 17:04
  • Yes, that should be the only line of code under the success block (unless you also need to do something else before the reload). Commented Sep 3, 2014 at 17:07
  • what about this line: var valuationUrl = window.location.protocol + "//" + window.location.host + "/ValuationList.aspx"; I did this so that when the website goes live, it can have HTTP or HTTPS, and any URL / Host. Commented Sep 3, 2014 at 17:11
  • 1
    Are you sure the success function is actually running? Commented Sep 3, 2014 at 17:17

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

window.location did not work. I had tried, but it still did not workout. location.assign() and location.replace() have already been used. But I cannot find any error after executing them.
1

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.

1 Comment

I did not have it before. I found it while I was trying to find a solution to my problem. I just added thinking may be some cache might be doing some prob. So added it, so as to enforce Javascript to be loaded from server. Page do not load without it also.
1

Try this.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.