많은 데이터를 MySQL 데이터베이스에 삽입하는 XMLHttpRequest가 있으며 5-10 초가 걸립니다. 다음은 요청의 코드입니다 :긴 XMLHttpRequest를 보낸 후 다른 페이지로 이동
function sendRequest(url, params, divToChange)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest)
{
var httpRequest = new XMLHttpRequest();
}
else // code for IE6, IE5
{
var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
// Stage 3 - open the request and prepare variables for the PHP method being
activated on server side//
httpRequest.open("POST", url, true);
//Set request headers for POST command//
httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpRequest.setRequestHeader("Content-length", params.length);
httpRequest.setRequestHeader("Connection", "close");
httpRequest.onreadystatechange=function()
{
if(httpRequest.readyState==3)
{
document.getElementById(divToChange).innerHTML='please wait';
}
if(httpRequest.readyState==4 && httpRequest.status==200)
{
document.getElementById(divToChange).innerHTML=httpRequest.responseText;
}
}
httpRequest.send(params); // Stage 4 - sending the request to the server//
//end when the server will responde we will execute the
"document.getElementById("txtHint").innerHTML=xmlhttp.responseText;" //
}
//Calling the request with this function//
function createEventList()
{
var url = "grid-update_event_list.php";
var params = "firstLoad=1";
//Create the httpRequest and send it to the server//
sendRequest(url,params, "eventsList");
}
는 그리고 이것은 응답 텍스트 얻을 내 HTML에 정의되어 있습니다 : 내 브라우저에서이 개 행동을 이해하지 못할
<p>Events List Status: <b id='eventsList'> </b></p>
:
1을 "기다려주십시오"라는 텍스트는 브라우저에 표시되지 않습니다. 어떤 일이 일어나는지 테스트하기 위해 alert(httpRequest.readyState)
을 추가하면 예상대로 "3"이라는 팝업이 나타 났지만 readyState==3
바로 다음에 표시되는 것은 readyState==4
입니다. 동시에 일어나는 것처럼 보입니다. httpRequest.responseText;
에 표시된 텍스트는 readyState==4
입니다.
2 요청을 보낸 후 다른 페이지로 이동할 수 없습니다. 현재 페이지가 응답 가능합니다 (콤보 상자를 변경할 수 있음). 다른 링크로 이동하려고 시도 할 때 브라우저는 readyState == 4로 대기 한 다음 계속합니다. 링크로. 나는 이해할 수 없다. 요청이 asynchronous = true
으로 정의 되었기 때문이다.
나는이 2 가지 행동을 유발하는 것으로 잘못 생각하고 있습니까? 감사.
첫 번째 동작은 jquery로 쉽게 제거 할 수 있습니다. jquery 당신이 취할 수있는 옵션이 무엇입니까? – itachi
아마도,하지만이 behaivor 이해하고 솔루션을 조작하는 대신 기지에서 해결하는 것이 더 좋습니다 ... –
'onreadystatechange' 함수 밖에서 "Please wait"를 설정하고 readyState가 4가되면 제거하십시오. ajax가 청크로 파일 청크를로드한다는 사실 때문에 발생할 수 있습니다. – d4rkpr1nc3