Fill div with ajax result

0

I have an ajax request with the following code:

<script type="text/javascript">        
    function AddVoto()
    {
        $.ajax({
        type: "POST",
        url: "{{url('votos/adicionar/')}}/{{{$postagem->id_postagem}}}",
        data: {'id_post':<?php echo $postagem->id_postagem;?>, '_token': $('input[name=_token]').val()},
        cache: false,
        success: function(data){
           alert("pedido feito com sucesso");// apresentar aqui o resultado
        }
        });
    }
</script>

<a href="#" style="text-decoration:none;" id="myVoto" onclick="AddVoto();return false;">Adicionar voto</a>
<div id="resultado"></div>

The request is arriving correctly, and I want to print the result of the in div "result" but I am not able to. How can I change the content of the div with the result of the ajax request?

    
asked by anonymous 24.06.2015 / 16:02

2 answers

2

If your function returns HTML code just send the same to div , in your success :

success: function(data){
           $("#resultado").html(data);
        }
    
24.06.2015 / 16:07
2

What you should do is pick up the date and put it inside div . I do not know how that is your object of return. If it's just a text, just put data inside the div that neither the code below.

     <script type="text/javascript">
            $('#myVoto').click(function(){ AddVoto(); return false; });

            function AddVoto()
            {
                $.ajax({
                    type: "POST",
                    url: "{{url('votos/adicionar/')}}/{{{$postagem->id_postagem}}}",
                    data: {
                        'id_post':<?= $postagem->id_postagem ?>, 
                        '_token': $('input[name=_token]').val()
                    },
                    cache: false,
                    success: function(data){
                        $('#resultado').html(data);
                    }
                });
            }
        </script>

        <a href="#" style="text-decoration:none;" id="myVoto" onclick="AddVoto();return false;">Adicionar voto</a>

        <div id="resultado"></div>
    
24.06.2015 / 16:07