2013-03-13 5 views
2

JQUERY Post 기능을 사용하는 데 문제가 있습니다.Jquery.post()에서 콜백이 호출되지 않았습니다.

저는 JQUERY Post 함수를 호출하는 2 개의 함수가 있습니다. 둘 다 잘 작동하지만 콜백 함수는 결코 호출되지 않습니다 (handleLike).

handleLike를 수동으로 호출하면 완벽하게 작동합니다. (handleLike 내부에 경고가 있어도 콜백 함수는 호출되지 않습니다.)

이 문제를 도와 주시겠습니까? 있다, 실수 없다면

$(document).ready(function() { 
    function handleLike(v_cb){ 
     alert("Call back chamou!"); 
     $('#erro').html(v_cb.mensagem); 
     var elemento = (v_cb.class && v_cb.class == 'map') ? $('#maplike') : $('#commentlike'+v_cb.id); 
     if (!elemento.hasClass('disabled')){ 
      var f = elemento.addClass("disabled").find('font'); 
      f.html(++Number(f.text())); 
     } 
    } 
    function ajaxError(jqXHR, textStatus, errorThrown) { 
     alert('$.post error: ' + textStatus + ' : ' + errorThrown); 
    }; 
    $('#maplike').on('click', function() { 
     var $this = $(this); 
     if (!$this.hasClass('disabled')) { 
      $.post('/cmap/maps/like', { id: $this.attr("name") }, handleLike, 'json').fail(ajaxError); 
     } 
    }); 
    $('[id*="commentlike"]').on('click', function() { 
     var $this = $(this); 
     if (!$this.hasClass('disabled')) { 
      $.post('/cmap/comments/like', { id: $this.attr("name") }, handleLike, 'json').fail(ajaxError); 
     } 
    }); 
}); 

검증되지 않은 :

<script type="text/javascript"> 
    $(document).ready(function() { 

     function handleLike(v_cb){ 


     alert("Call back chamou!"); 
     $('#erro').html(v_cb.mensagem); 

     if (v_cb.class == 'map'){ 
      var elemento = $('#maplike'); 
     }else{ 
      var elemento = $('#commentlike'+v_cb.id); 
     } 


     if (!(elemento.hasClass('disabled'))){ 

      elemento.addClass("disabled"); 
      var likes = elemento.find('font').text(); 
      likes++; 
      elemento.find('font').html(likes); 
     } 
     } 

     $('#maplike').click(function() { 

      //var map_id = $('#like').find('font').attr('value'); 

      var id = $(this).attr("name"); 




      if (!($(this).hasClass('disabled'))){ 

      var JSONObject= { 
       "mensagem":"Testando Json", 
       "id":86, 
       "class":"map" 
      }; 

      handleLike(JSONObject); 

      alert("Teste"); 

      $.post(
       '/cmap/maps/like', 
       { id: id }, 
       handleLike, 
       'json' 
      ); 
      } 
     }); 

     $('[id*="commentlike"]').click(function() { 

      //var map_id = $('#like').find('font').attr('value'); 

      var id = $(this).attr("name"); 


      if (!($(this).hasClass('disabled'))){ 

      $.post(
       '/cmap/comments/like', 
       { id: id }, 
       handleLike, 
       'json' 
      ); 


      } 
     }); 


    }); 

    </script> 
+1

콜백은 어디에서 불 렸습니까? 콜백 함수 호출이 문제 일 가능성이 높습니다. –

+0

Ajax 요청이 올 바르고 게시됩니까? 그것은 실패 할 수 있습니다. 따라서 콜백이 트리거되지 않습니다. –

+1

서버가 유효한 json을 반환하지 않는 것 같습니다. 이러한 요청의 오류 처리기를 사용했다면 아마도 ""parseerror "' –

답변

4

진단하지 솔루션

합리화 및 오류 처리기를 추가, 당신은 이런 식으로 뭔가를 얻어야한다 오류 처리기가 잘못된 정보를 알려줄 수있는 좋은 기회입니다.

1

나는 Kevin B 팁을 따라 $ ajax 메서드를 사용한다.

이것은 구문 오류였습니다. 죄송합니다.

v_cb의 반환은 json이 아니며 html입니다. 나는 나의 복귀를 바로 잡는다. 그리고 모든 것은 ok이었다.

관련 문제