Can I test if URL is reachable using AJAX + cross-domain + jsonp?

前端 未结 1 1499
醉话见心
醉话见心 2021-01-12 17:54

I\'m using JQuery to fetch information from an URL and display it on my page asynchronously. The URL comes from other domain, so I use JSONP to get the data. That works fine

相关标签:
1条回答
  • 2021-01-12 18:41

    add a timeout

    $.ajax({
            type : "GET",
            url : "http://otherdomain.com/somePage.html",
            data : params,
            timeout:3000,
            dataType : "jsonp",
            jsonp : "jsonp",
    
            success : function (response, textS, xhr) {
                alert("ok");
            },
            error : function (xmlHttpRequest, textStatus, errorThrown) {
                alert("not ok " + errorThrown);
                 if(textStatus==='timeout')
                  alert("request timed out");
            }
        });
    

    DEMO

    0 讨论(0)
提交回复
热议问题