简体   繁体   中英

Callback never called on Jquery.post();

I'm having some trouble using JQUERY Post function.

I have 2 functions that call JQUERY Post function. Both of them is working fine, but the callback function is never called (handleLike).

When I call handleLike manually, it's works perfect. (Even if handleLike has just an alert inside, the callback function is not called)

Could you please help me with this thing?

<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>

Diagnostic, not solution

Rationalizing and adding an error handler, you should get something like this :

$(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);
        }
    });
});

untested

Barring mistakes, there's a good chance the error handler will inform you of what's going wrong.

I follow the Kevin B tip and use $ajax method.

It was a parseerror. Sorry.

The return of v_cb was not a json, it was a html. I correct my return, and everything was ok.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM