如何编码ajax获取数据?

时间:2013-10-08 16:39:01

标签: codeigniter

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

});});  

但在url中是:localhost / project / workplace / test / menu?3

如何制作:localhost / project / workplace / test / menu / 3

2 个答案:

答案 0 :(得分:0)

尝试替换

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

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

但这不是最佳解决方案。

答案 1 :(得分:0)

如果要从URL中删除?3,请使用POST而不是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);
    }
});

这将使URL localhost / project / workplace / test / menu /