How to prevent ajax requests to follow redirects using jQuery

前端 未结 9 553
梦如初夏
梦如初夏 2020-11-27 14:37

I use the jQuery ajax functions to access a web service, but the server, instead of returning a response with a status code describing a problem, the request is redirected t

相关标签:
9条回答
  • 2020-11-27 15:12

    I suppose you receive a 200 response because the second time there is no redirection, because the 404 page does not expire, it is saved in the cache. That is to say that the second time the browser gives you the page in the cache. There is a property "cache" in the ajax jquery. http://api.jquery.com/jQuery.ajax/

    You should write it to "false"

    0 讨论(0)
  • 2020-11-27 15:17

    While it is not possible to disable location redirect following in XmlHttpRequests, it is when using fetch():

    fetch('url', {redirect: manual});
    
    0 讨论(0)
  • 2020-11-27 15:19

    I'm not sure if this will apply in your case, but you can write code to respond to specific status codes in AJAX function -

    $.ajax({
        url: '/admin/secret/data',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        statusCode: {
            200: function (data) {
                alert('302: Occurred');
                // Bind the JSON data to the UI
            },
            401: function (data) {
                alert('401: Occurred');
                // Handle the 401 error here.
            }
        }
    });
    
    0 讨论(0)
  • 2020-11-27 15:24

    I was interested in the same thing and could not find the state() method mentioned by Takman and did a little digging for myself. For the sake of people turning up here in search of an answer, here are my findings:

    As stated multiple times, you cannot prevent redirects, but you can detect them. According to MDN you can use the responseURL of the XMLHttpRequestObject, which will contain the final URL the response came from, after all redirects. Only caveat is that it is not supported by Internet Explorer (Edge has it). Since the xhr/jqXHR passed into the success/done function of jquery is an extension of the actual XMLHttpRequest, it should be available there, too.

    0 讨论(0)
  • 2020-11-27 15:24

    In the request headers in the case of ajax request you will have the following

    X-Requested-With    XMLHttpRequest
    

    By this criteria on the server side you can filter requests.

    0 讨论(0)
  • 2020-11-27 15:25

    I don't believe it is possible. The underlying library (XHR) makes the new request transparently. That being said, what I have done in these situations (usually a session-timeout type of deal that takes me to a login page) is send back a custom response header. I also have setup a global ajax handler that checks for the presence of that header, and responds appropriately when present (for example, redirecting the whole page to the login screen).

    In case you're interested, here's the jQuery code I have to watch for that custom header:

    /* redirects main window when AJAX request indicates that the session has expired on the backend. */
    function checkSession(event, xhr, ajaxOptions)
    {
        if (xhr.readyState == 4)
        {
            if(xhr.getResponseHeader("Login-Screen") != null && xhr.getResponseHeader("Login-Screen").length)
            {
                window.location.href='sessionExpired.html'; //whatever
            }
        }
    }
    
    $(document).ajaxComplete(checkSession)
    
    0 讨论(0)
提交回复
热议问题