JS | jQuery ajax 중첩, 폼 넘어가기 막기, Return false 변수 처리법

|

ajax 중첩 사용

let check = true;

$.ajax({
  url: "login.php",
  type: "POST",
  async: false, /* true 동기 false 비동기 | 지원 종료 예정 > await fetch 사용 */
  data: {
    id: check
  },
  dataType: "json",
  success: function(response) {
    let checkLogin = response.secret;
    $.ajax({
      url: "login.php",
      type: "POST",
      async: false,
      data: {
      id: check
    },
    dataType: "json",
    success: function(response) {
      alert(checkLogin);
    }
}

JS 함수 return false 처리하기

function appleFarm() {
  let ajaxReturnCheck = true;

  /* JS 리턴값 처리 */
  if(true){  
    ajaxReturnCheck = false;
  }

  /* JS 리턴값 처리 */
  if(true){  
    ajaxReturnCheck = false;
  }

  /* 함수 마지막에 false 처리 */
  if (ajaxReturnCheck === false){
     return false;
  }
}

return false 를 중간 중간에 넣으면 정상적으로 실행이 안될 수 있습니다.
이를 방지하기 위해 위와 같이 false 변수 처리를 할 수 있습니다.

form 전달 중단

event.preventDefault(); // 폼 넘어가지 않게 막기

응용

document.getElementById("login").submit(function(event){
  event.preventDefault(); // 폼 넘어가지 않게 막기
}

로그인 처리를 위해 사용합니다.

다른 방법

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!--    <script>-->
<!---->
<!---->
<!--        c_code = '16035';-->
<!--        re_type = 1;-->
<!--        re_it_id = 166259620;-->
<!--        coupon_end_dt = '2022-12-31';-->
<!--        cp_id = 4030;-->
<!---->
<!--        $.ajax({-->
<!--            type: 'POST',-->
<!--            url:  '../../cm/shop/ajax_auto_make_off_coupon.php',-->
<!--            data : { c_code :16035 ,re_type:re_type , re_it_id:re_it_id, coupon_end_dt:coupon_end_dt},-->
<!--            dataType: 'json',-->
<!--            error: function(err){-->
<!--                console.log (err);-->
<!--            },-->
<!--            success: function(rep){-->
<!--                console.log ("잘가져옴"+rep);-->
<!---->
<!--                // alert(JSON.stringify(rep));-->
<!--            }-->
<!--        });-->
<!--    </script>-->

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

클릭하여 복사