모든 Ajax 기반 웹 사이트를 생성하므로 모든 작업에서 다른 JS 함수를 호출하므로 각 함수에서이 Ajax 코드를 사용하여 함수를 큰 코드로 만듭니다. 단축 단축 코드 AYX 코드
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var getData=xmlhttp.responseText;
if(getData=="something") {
/*
code goes here
*/
}
else {
/*
code goes here
*/
}
}
}
xmlhttp.open("GET","mypage.php",true);
xmlhttp.send();
그래서 나는 단지 아약스 코드 위에 포함 된 다른 기능을 사용하고 난 필요할 때마다 그래서 그것을 호출 할 필요가 세계적으로 내 변수 GetData의를 선언해야 물어보고 싶었다. 여기
내가 그것을 실행중인 다른 작업과 충돌을 만들 것인가
var getData=""; /*declaring var Globally (I read it like this dont know right)*/
function oneAjax(checkPage) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
getData=xmlhttp.responseText;
/*now check further in the function which called it*/
}
}
xmlhttp.open("GET",checkPage+".php",true);
xmlhttp.send();
}
을 사용하고 싶었 어떻게? 또는 내 문제에 대한 올바른 해결책을 제공하십시오.
function oneAjax(checkPage, done, fail) {
...
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
done(xmlhttp.responseText, xmlhttp.status);
} else {
fail(xmlhttp.status);
}
}
};
}
는 요구 사항에 맞도록 콜백에 전달 된 매개 변수를 조정 : 당신은 기성품 라이브러리를 사용하지 않을 경우
'oneAjax'를 어떻게 호출해야하는지에 대한 예제를 제공 할 수 있습니다. 콜백은 초보자에게 혼란을 줄 수 있다고 생각합니다. 튜토리얼의 경우 –