Detect HTTP 301 status code from JavaScript

本小妞迷上赌 提交于 2019-12-23 12:26:29

问题


I need to detect client side if a requested file (with XMLHttpRequest) had a 301 response. The reason of doing this is because I need to request other files related to the file where user has been redirected and not related to the first one requested. Any way to detect this response status code using JavaScript or JQuery?

Thanks.


回答1:


jQuery ajax callback function gives out a lot of info

$.ajax({
    url: "test.php",
    type: "GET",
    data: {},
    dataType:"json",
    success: function(resp, textStatus, xhr) {
        //status of request
        console.log(xhr.status);
    },
    complete: function(xhr, textStatus) {
        console.log(xhr.status);
    }
});



回答2:


You can use $.ajax with jquery

It has everything you need here : http://api.jquery.com/jQuery.ajax/

If you look at "statusCode" explaination, you will see you can make something for every code



来源:https://stackoverflow.com/questions/17863062/detect-http-301-status-code-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!