简体   繁体   中英

Why does an AJAX call sometimes redirect the page?

I have a page that contains 3 iframes, the top header, left navigation and the content iframe.

(this is a legacy application).

The page that contains the 3 iframes makes an jQuery AJAX call to a page:

/users/getNotifications.aspx

Sometimes (I believe when I am not logged in) the browser redirects to the URL of my AJAX call ie localhost/users/getNotifications.aspx

I obviously don't want it to redirect like this, any idea why this may be happening?

Note:

My page responds with:

 Response.ContentType = "text/html";
 Response.Write(resultInHtml);

Also, the page doesn't redirect if the user isn't logged in.

This is the method I call to get the data using an ajax requesT:

var x = function () {
        $.ajax({
            type: "GET",
            url: webRoot + "users/getNotifications.aspx",
            cache: false,
            success: function (payload) {
                if (payload.length > 0) {
                    $("#users-popup").html(payload);
                }
                displayPopup();                
            }
        });
    }

The problem is likely that your javascript doesn't deal well with whatever result comes back from your page when you are not logged in. Possibly it is trying to extract a value that doesn't exist in this context and thus throws an error.

This will then mean it doesn't finish running the function so any "return false" or other method of preventing default behaviour is not running and so the default click behaviour is occuring and navigating to a new page.

Without more code though that is just a theory.

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