开发者

call a method with jQuery AJAX

I have this code to try and call a method from my controller in codeigniter,

$("#Blog").click(function () {
       var url = $(this).attr("href");
        $.ajax ({
            url: "index.php/home/category",
            type: "P开发者_如何学PythonOST",
            success : function (html) {
                $("#right-content").append(html);
            }
        });
    });

The ajax does not seem to be getting fired, is there something that I am missing, the #Blog represents the ID of a link in my navigation menu, all that happens is the link works as normal.

Someone please help :-(


You need to prevent the normal link behavior from taking place.

$("#Blog").click(function (e) {
    var url = $(this).attr("href");

    // Block the normal click action
    e.preventDefault();

    // Create post
    $.post (
        "index.php/home/category", // <- request URI
        {url: url},                // <- any data goes here
        function (html) {          // <- callback
            $("#right-content").append(html);
        }
    });
});

Also, you might want to check out the documentation for $.post.


If you are overridding clicking on anchors, remember to block the default behavior of the link (i.e. jumping to the linked page).

So you might start with a change:

$("#Blog").click(function (ev) {
           ev.preventDefault();
           var url = // ... the rest as before.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜