如何在XHRHttpRequest完成或返回响应时调用函数seccuss。 不使用jquery。
xmlhttp = new XMLHttpRequest();
xmlhttp.success()
这种函数将在每次请求后调用
答案 0 :(得分:0)
您可以使用以下代码。
var ajaxRequest;
try{
// Opera 8.0+, Firefox, Safari, Chrome
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
console.log('something went wrong. Check the error! '+ e);
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var response = ajaxRequest.responseText;
// do something here with the response object
// you could JSON.parse to parse the response if it is json string
}
}
ajaxRequest.open("GET", "get_data.php", true); // true for asynchronous request
ajaxRequest.send(null); // initiate a send(call)
答案 1 :(得分:-1)
xmlhttp.onreadystatechange = function(){
if(request.status===200){
var response = xmlhttp.responseText;
//do something with response
}
}