How to prevent ajax requests to follow redirects using jQuery

前端 未结 9 647
梦如初夏
梦如初夏 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: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.
            }
        }
    });
    

提交回复
热议问题