how to codeigniter ajax get data?

2019-08-26 19:30发布

问题:

HTML

<div id="tabs">
            <ul id="category">
<li><a href='#' class='cate' id='3'>3</a></li></br>
            </ul>
</div>

Jquery

$(function() {
$(".cate").click(function()
{


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

     $.ajax({
       type: "GET",
       url: "<?php echo base_url('workplace/'.$username.'/menu') ?>",
       data: user_id,
       success: function(result){
           $("#category-details").html(result);
           }
     });

});});  

but in url is : localhost/project/workplace/test/menu?3

how to make : localhost/project/workplace/test/menu/3

回答1:

Try replace

url: "<?php echo base_url('workplace/'.$username.'/menu') ?>",

this

url: "<?php echo base_url('workplace/'.$username.'/menu/') ?>" + user_id,

but it isn't best solution.



回答2:

If you want to remove the ?3 from the URL, use a POST instead of a GET:

jQuery.ajax({
    type: "POST",
    url: "<?php echo base_url('workplace/'.$username.'/menu') ?>",
    data: user_id,
    error : function(result){
        // Do something here to see what the problem is
        // maybe console.log(result);
    },
    success : function(result){
        $("#category-details").html(result);
    }
});

This will make the URL localhost/project/workplace/test/menu/



标签: codeigniter