2014-03-06 3 views
0
내가 응답 데이터가 빈 JSON 개체 때 아약스 요청을 다시 시도하려는

에 아약스 요청을 다시 시도 할 수 있습니다.어떻게 빈 응답

내 아약스 코드는 다음입니다.

$.ajax({ 
     type: "POST", 
     url: "index.php?r=funding/getPaymentButton", 
     data: {email:benfEmail,data:JSON.stringify(data),paymentType:method}, 
     async: true,//false 
     success: function (data) 
     { 
      if(data.length === 0) 
      { 
       $(this).ajax();//retry ajax request here, if data is empty 
       console.log('err'); 
      } 
      else 
      { 
       obj = jQuery.parseJSON(data); 
       // prcessing ajax request 
      } 
     } 
}); 

PHP

echo json_encode(array(
    'type'=>$paymentType, 
    'btn'=>$this->PaymentBtn, 
    'usd' => round($this->finalUSDAmount,2),) 
); 
+2

왜 함수의 아약스 호출을 포장하지? 당신은 단순히 내가 @zoranc에 동의 나는 대신에 $의 아약스 호출 (이) 아약스() 라인 – zoranc

+0

내에서 함수를 호출 할 수 있습니다 – M98

답변

0

당신은 기능에 아약스 요청을 넣고 응 답이 비어있는 경우 그 함수를 호출 할 수 있습니다. 이처럼

:

function yourCall(){ 
    $.ajax({ 
     type: "POST", 
     url: "index.php?r=funding/getPaymentButton", 
     data: {email:benfEmail,data:JSON.stringify(data),paymentType:method}, 
     async: true,//false 
     success: function (data) 
     { 
      if(data.length === 0) 
      { 
       yourCall(); 
       console.log('err'); 
      } 
      else 
      { 
       obj = jQuery.parseJSON(data); 
       // prcessing ajax request 
      } 
     } 
    }); 
} 

이 설정은 그러나 유효한 응 답을 얻는다 전까지 아약스 전화를 계속 시도 할 것이다. 이 발사 유지하는 것이 의미 경우 응 답 길이 == 0

0

그냥 간단하게 다음을 수행하십시오

$.ajax({ 
    url : 'index.php?r=funding/getPaymentButton', 
    type : 'POST', 
    data : {email:benfEmail,data:JSON.stringify(data),paymentType:method}, 
    tryCount : 0, 
    retryLimit : 3, 
    success : function(json) { 
     //do something 
    }, 
    error : function(xhr, textStatus, errorThrown) { 
     if (textStatus == 'timeout') { 
      this.tryCount++; 
      if (this.tryCount <= this.retryLimit) { 
       //try again 
       $.ajax(this); 
       return; 
      }    
      return; 
     } 
     if (xhr.status == 500) { 
      //handle error 
     } else { 
      //handle error 
     } 
    } 
});