Notice
                              
                          
                        
                          
                          
                            Recent Posts
                            
                        
                          
                          
                            Recent Comments
                            
                        
                          
                          
                            Link
                            
                        
                    | 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | 
| 12 | 13 | 14 | 15 | 16 | 17 | 18 | 
| 19 | 20 | 21 | 22 | 23 | 24 | 25 | 
| 26 | 27 | 28 | 29 | 30 | 31 | 
                            Tags
                            
                        
                          
                          - JavaScript
- MySQL
- myshortcut
- 부트캠프후기
- DOM
- 배열
- error
- JDBC
- OOP
- jQuery
- SQL
- react
- 노션
- formula
- 객체지향
- dao
- php
- 오류
- 멀티캠퍼스it부트캠프
- Excel
- 현대이지웰java풀스택개발자아카데미6월
- explode()
- ES6
- strpos()
- DTO
- 정규식
- 함수
- node.js
- Java
- oracle
                            Archives
                            
                        
                          
                          - Today
- Total
코딩짜는 일상
[javascript, ajax] 함수에서 ajax 결과값이 반환되지 않을 때_async 옵션 본문
      IT/Javascript&Jquery
      
    [javascript, ajax] 함수에서 ajax 결과값이 반환되지 않을 때_async 옵션
Remily 2023. 10. 19. 13:00반응형
    
    
    
  서론
변수를 받아 특정 칼럼을 업데이트할 목적으로
function update_column(){
    let event = false;
    $.ajax({
        url: "주소",
        data: {'변수값': 변수값},
        type: 'POST',
        dataType: 'JSON',
        success: function(res){
            if(res){
                const code = res['resultCode'];
                const msg = res['resultMsg'];
                event = true;
            } else{ alert("회신내용 없음"); }
        }, error: function(xhr, textStatus, errorThrown){ alert("jqXHR:"+xhr+"\ntextStatus:"+textStatus+"\nerrorThrown:"+errorThrown); }
    });
    return event;
}
이렇게 함수를 만들었는데
ajax 통신을 성공해도 함수의 반환값은 false 였습니다.

결론
결론만 말하자면 ajax는 비동기가 Default이기 때문에
async 속성을 false 해주어야 동기식으로 처리를 합니다.
function update_column(){
    let event = false;
    $.ajax({
        url: "주소",
        data: {'변수값': 변수값},
        type: 'POST',
        dataType: 'JSON',
        async: false,
        success: function(res){
            if(res){
                const code = res['code'];
                const msg = res['msg'];
                event = true;
            } else{ alert("회신내용 없음"); }
        }, error: function(xhr, textStatus, errorThrown){ alert("jqXHR:"+xhr+"\ntextStatus:"+textStatus+"\nerrorThrown:"+errorThrown); }
    });
    return event;
}
오늘 또 한 건 배웠습니다.

반응형
    
    
    
  'IT > Javascript&Jquery' 카테고리의 다른 글
| [Javascript] 부분 문자열 포함 여부 확인 - php strpos로 고쳐보기 (0) | 2024.01.24 | 
|---|---|
| [Javascript] 가격 표시, 천단위 콤마 정규식 + 숫자만 가져오기 정규식 (1) | 2024.01.24 | 
| [Javascript] console.log 동작 안 하는 이유 - filter (0) | 2023.06.26 | 
| [Javascript & jQuery] 애니메이션 메뉴 접기/펴기(collapse) - 아코디언 메뉴 (0) | 2022.09.29 | 
| [Javascript+html] scrollTo 안 먹히는 이유 - 찔끔 움직이고 끝나는 이유 / 움직일 스크롤 특정 (0) | 2022.07.29 | 

