0

본인의 대학에서 세션이 만료되는 시간을 알기 위해이 코드를 사용하고 있습니다.Google 크롬에서 리소스를 사용할 수 있는지 확인하십시오.

function checkRemaining() { 
    var xhReq = new XMLHttpRequest(); 
    xhReq.open("GET", "http://192.168.168.168/dynUserLogin.html?loginDone=1", false); 
    xhReq.send(); 

    var regex = /Login session time remaining: ([0-9]+) minutes/g; 
    var res = regex.exec(xhReq.responseText); 

    return res[1]; 
} 

그러나 대학 인터넷에 연결되어 있지 않으면 오류가 발생합니다.

대부분의 사람들은 AdBlock 을 사용하고 있었기 때문에이 오류를 검색했을 때도 있었지만 제 경우에는 (다른 사람들도 가지고 있다고 생각합니다) 때때로 자원을 사용할 수 없을 수도 있습니다. 요청이 실패 할 경우 함수를 트리거하는 방법은 무엇입니까?

chrome status failed

답변

1

XMLHttpRequets tell you what the result was 것이다.

if (xhReq.status === 200) { 
    // perfect! 
} else { 
    // there was a problem with the request, 
    // for example the response may contain a 404 (Not Found) 
    // or 500 (Internal Server Error) response code 
} 
관련 문제