내가하려는 작업 : 1. 초기에 일부 입력을 기반으로 서버에 ajax 요청을 제공합니다. 2. 서버가 RQ (Python-rq)에서 생성 한 작업 ID를 반환합니다. 3. URL에 대한 작업 ID를 기반으로 ajax 요청 유효한 응답을 얻을 때까지 정기적으로 jobid로 구성
내가 가진 것 :
$.ajax({
type: "POST",
url: "/start",
data:{crop: valueCrop, state: valueState, variablemeasure: valueVariable, unit:unitMeasure, from:yearFrom, to:yearTo},
success: function(results) {
console.log(results);
var jobId='';
jobId = results;
function ajax_request() {
$.ajax({
type: "GET",
url: "/results/" + jobId,
dataType: "json",
success:function(xhr_data) {
if (xhr_data == {"status":"pending","data":[]}){
console.log("Waiting for response");
setTimeout(function() { ajax_request(); }, 2000);
} else {
console.log(xhr_data);
}
},
error:function(error) {
console.log(error)
}
});
}
},
error: function(error) {
console.log(error)
}
})
이것이 가능할까요? rq가 작업이 완료되었다고 말하지만 콘솔에 출력이 전혀 표시되지 않습니다. 나는 그것이 if 루프에 들어 가지 않는다고 생각합니다. "/ results / jobId"URL을 방문하면 결과를 볼 수 있습니다. 도와주세요.
이 코드에 몇 가지 버그가 있습니다. 먼저 함수를 정의했습니다 ajax_request()
. 그러나 당신은 그것을 부르지 않습니다. 정의 끝 부분에서 호출 할 수 있습니다.
둘째,이 코드는 문제가 있습니다.
if (xhr_data == {"status":"pending","data":[]})
객체 표기법은과 같지 않은 또 다른 객체를 만듭니다 xhr_data
.
넌 할 수있어:
if (xhr_data.status === "pending")
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다