2014-07-22 5 views
0

내부 서버에 저장된 네트워크를 통해 웹 사이트를로드 할 때 성능 문제가 있습니다. 성능 문제는 내 Javascript 파일에서 여러 AJAX 요청이 끊임없이 XML 파일에 액세스하려고 시도한 다음 XML 노드에서 이미지를 가져 오거나 XML 노드에서 이름을 가져 오는 것과 같습니다.Javascript AJAX 요청 병합

AJAX 요청을 병합하여 클라이언트 장치에서 서버에 저장된 XML 파일 요청을 줄일 수있는 방법이 있습니까?

내 코드는 다음과 같습니다 :

function getimage(getID,XMLImageID) 
{ 
$("#" + getID).empty(); 
$.ajax({ 
type: "GET", 
url: "XML/XML.xml", 
dataType: "xml", 
success: function(xml){ 
$(xml).find(XMLImageID).each(function(){ 

var image = $(this).find("image[href]").attr("href"); 
$('#'+ getID).append($("<img"+" class="+"Timages"+" src=\"" +image+"\"/>")); 
});   
}, 
error: function() { 
alert("(getImage) An error occurred while processing XML file. Reasons could be: \n The XML cant be located. \n The XML is being used by another program. \n The XML is trying to parse incompatible characters. \n The XML syntax/tags are incorrect."); 
} 
}); 
} 

function getdept(getid,XMLID) 
{ 
var scriptTag = document.scripts[document.scripts.length - 1]; 
var parentTag = scriptTag.parentNode; 
getid = parentTag.id; 
$.ajax({ 
type: "GET", 
url: "XML/XML.xml", 
dataType: "xml", 
success: function(xml){ 
$(xml).find(XMLID).each(function(){ 
var dept = $(this).find('Dept').text();    
var id = $(this).attr("id"); 
var sName = $(this).find('Name').text(); 
$("#"+getid).append('<div class="Team_People" onmouseover="area_hover1(this);area_hover2(this)" href="#p'+id+'">'+dept+'<br />'+sName+'</div>'); 

}); 
}, 
error: function() { 
alert("(getHierPeopleDept)An error occurred while processing XML file. Reasons could be: \n The XML cant be located. \n The XML is being used by another program. \n The XML is trying to parse incompatible characters. \n The XML syntax/tags are incorrect."); 
} 
}); 
} 

Ajax 응답은 아래의 간단한 코드를 사용, 난 그냥 더 깔끔한보다는 가진 여러 아약스 요청을 위의 코드를 병합해야합니다. 여러 매개 변수를 추가해도 작동 할 수 있는지 여부는 확실하지 않습니다.

$.ajax({ 
type: "GET", 
url: "XML/XML.xml", 
dataType: "xml", 
success: function(xml){ 
$(xml).find(XMLID).each(function(){ 
//Get something from XML node 
}); 
}, 
}); 
} 

누구든지 올바른 방향으로 나를 안내 할 수 있다면 그것은 대단히 감사 할 것입니다! 감사 감사 AJ

+0

난 당신이 캐싱 부분에 작업을하여 시작하는 것이 좋습니다 글로벌 XML 변수와 쿼리를 만듭니다. 당신의 객체가 서버의 응답 헤더에서 캐시 가능하다고 선언되었는지 확인하십시오. – njzk2

답변

1

은 함수 호출로 ...

var XML = null; 

function fetchXML(){ 
    $.ajax({ 
     type: "GET", 
     url: "XML/XML.xml", 
     dataType: "xml", 
     success: function(data){ 
      XML = data; 
     }, 
     error:function(){ alert('something went wrong');}) 
} 

function getImage(id) { 
    $(XML).find(id).each(){ ... }; 
} 

funcition getDept(id) { 
    $(XML).find(id).each(){ ... }; 
} 
+0

나는 fetchXML 함수를 넣기 만하면 원래의 JavaScript Ajax XML 요청의 나머지 부분을 멈추게한다. – AlpaxJ1

+0

문제를 발견했습니다. 크롬에 Async : false 헤더를 사용해야했습니다. 당신의 도움을 주셔서 감사합니다! – AlpaxJ1