나는이 질문을 받았을 거라는 걸 압니다, 아마 백만 번이나, 그러나 나에게는 일을 아무것도 할 수 없습니다.다른 코드로 이동하기 전에 비동기 아약스가 완료 될 때까지 기다리시겠습니까?
"변경된"이벤트에서 모델의 유효성을 검사하는 UI 마법사 컨트롤이 있습니다. 모델이 유효하지 않으면 사용자가 마법사에서 앞으로 이동할 수 없습니다. jquery에서 $ .when(). done() 기능을 사용하여 피곤했지만 코드가 유효한지 확인하기 전에 코드가 여전히 통과됩니다. 비동기 아약스 요청을 호출하는 이유는 UI를 잠그지 않아서 일종의 진행 표시기를 표시 할 수 있기 때문입니다. async 속성을 false로 설정했지만 내 UI 표시기가 나타나지 않습니다.
//the change event that is called when the user clicks 'next' on the wizard:
wizard.on('change', function (e, data) {
var isValid = $.validate({
"Model": [The_UI_MODEL],
"Url": [URL_To_Server_Validation],
"Async": true, //tells ajax request to send as async
});
//Tells the wizard not to move 'next' if the request comes back as not valid
if (data.direction === 'next' && !isValid) {
e.preventDefault();
}
}
JQuery와 내 시스템에 어떤 모델의 유효성을 검사하는 함수를 작성하는 // 나는 $ .extend 방법을 사용하고 있습니다 : 여기 내 코드가 무엇을하고 있는지의 예입니다.
postJson: function(options){
...other code for my application
//This is where I want the ajax request to happen and once done return the data coming back
//This is what I have tried, but it is not doing what I thought.
$.when(function(){
return $.ajax({
url: options.Url,
type: 'POST',
cache: false,
async: options.Async,
timeout: options.Timeout,
contentType: 'application/json; charset=utf-8',
dataType: "json",
data: JSON.stringify(options.Model),
error: function(xhr, status, error) {
...do stuff if ajax errors out
},
success: function (data) {
},
});
}).done(function(response){
//looks like i get back the responseText of the request. which is fine, but other posts i have read stated i should be getting back the request itself
...other UI stuff
return response;
})
}
당신의 행동을 수행하는 함수를 작성하고 그 함수를 성공적으로 호출하면 성공한 것입니다 : success : function (data) {...}'... – KarelG
@KarelG, change 이벤트가 먼저 발생하고 validate . ajax 요청은 async이기 때문에 $ .validate 함수는 즉시 호출됩니다. 'var isValid'는 항상 false입니다. – DDiVita