Fill div with ajax result

Asked

Viewed 2,260 times

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 coming in correctly, and I want to print the result on the div "result" but I’m not getting it. How can I change the contents of the div with the result of the ajax request?

2 answers

2

What you should do is take the date and put inside the div. do not know how this your return object. If it is just a text, just put the data even 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>

2


If your function returns HTML code just send it to div, in your Success:

success: function(data){
           $("#resultado").html(data);
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.