여기를 묻는 생각을하기 전에 잠시 동안 답을 찾고 있었지만 방금 막혔습니다.jQuery post() true 또는 false를 반환합니다.
내가 내와 비슷한 기사/질문을 읽을 수 있지만 결국이 한 다음했습니다
- JQuery - Storing ajax response into global variable // 답변 : https://stackoverflow.com/a/905324
- jQuery: Return data after ajax call success
그리고 이걸로 끝났다 :
var del_act = (function(){
var resp;
$.post(url+'delete/'+id, 'json')
.done(function(data) {
custom_alert(type.toUpperCase()+' deleted succesfully!');
if(redirect)
window.location.replace(url);
resp = true;
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
if(jqXHR['status'] == 404)
custom_alert(type + ' not found');
resp = false;
});
return {getResp: function()
{
console.log(resp);
return resp;
}};
})();
console.log(del_act.getResp());
하지만 oblem은 모두 console.log
이 undefined
응답을 반환한다는 것입니다. 내가 을 async: false,
과 함께 사용할 수 있다는 것을 알고 있지만 원하지 않습니다. 따라서 true
또는 false
응답을 반환하는 방법을 찾으려고합니다.
Err 예, 잠시 동안 문서를 확인하지 않았지만 성공했습니다. 요즘 멋진 점은 콜백에 대한 아이디어를 얻는 것이 중요하다는 것입니다. –
문제는 내가'doneCB (true)'를 호출 할 때'undefined is not function'을 얻게된다는 것입니다. – Alex
잘, del_act에 매개 변수로 doneCB를 전달해야합니다. –