Redirect Com Jquery

Asked

Viewed 1,004 times

0

I’m making a system that makes a requisition ajax with Xios. So far td well, I was able to make the request GET and list all items on the screen in a table. In this table there is a button View Item, that I should redirect to another page where I will show only the item in question. I do this with PHP, but I’m studying Javascript and would like to know what is the best way to do that.

axios.get('http://127.0.0.1:8000/api/post')
.then(function(response){
    jQuery.each(response.data, function(key, value){ 
        //REQUISIÇÃO AJAX COM AXIOS
        jQuery.each(value, function(label, answer){                 
            display = '\
                <tr>\
                    <td>' + value.id + '</td>\
                    <td>' + value.title + '</td>\
                    <td>\
                        <button id="btn'+ value.id +'" class="btn btn-primary">Ver detalhes</button>\
                    </td>\
                </tr>'
            }); 
            $('#tbody').append(display);

            //BOTÃO QUE DEVE REDIRECIONAR PARA OUTRA PÁGINA
            $('#btn' + value.id ).click(function() {
                window.location.href = "post.html";
            });
        }); 
});
  • that’s right... window.location.href, what exactly is your doubt?

  • Why instead of creating a button you don’t create a link (<a>) with the attribute href? :)

  • my question is how to pass the parameters to the other page, I need to get a get with the item id or I can send the parameters directly from that page and just read it on the other page. If yes, how to do this??

1 answer

0

Ideally you send the id to the page via query string example:

www.seusite.com/detalhePost.php?id=1

On the other page you should take this id and fetch its details with another request, be ajax, axios or php same. and from there you show the details of your post.

you can also send the whole object by the url note:

inserir a descrição da imagem aqui

I suggest you use a pattern called REST API

About Rest API

and instead of www.seusite.com/detalhePost.php?id=1 Voce can do www.seusite.com/detalhePost/1

and make good use of the methods HTTP => GET, POST, PUT e DELETE

Browser other questions tagged

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