Monday 3 September 2018

Call PHP AJAX does not recover anything

I am trying to retrieve HTML and display it on the page, it doesn't seem to retrieve anything...

My Controller script:
function index()
{
    $this->load->view('ajax_post');
}

public function ajaxify()
{
    $message = "<b>hello</b>";
    echo $message;
}

My javascript code:
  $(document).ready(function(){
    $("#click").click(
    function(){

        $.ajax({
        type: "POST",
        url: "/index.php/ajax_post/ajaxify",
        dataType: "html",
        cache:false,
        success:
          function(data){
            $("#content").append(html);
          }

        });

      return false;

    });

});

any help would be appreciated, as I'm an absolute beginner with jQuery/AJAX
Thanks

Your URL is built wrong; if you see the NET console in Firebug it would show 404 (or 500). Use the helper function in CI (url helper):
$.ajax({
    type: "POST",
    url: "<?php echo site_url('ajax_post/ajaxify');?>",
    dataType: "html",

or build it manually including the domain name:
$.ajax({
    type: "POST",
    url: "http://www.yourdomain.com/index.php/ajax_post/ajaxify",
    dataType: "html",

Be sure the url maps to the correct controller/method (ajax_post.php controller, ajaxify method if no custom route is assigned)
Sidenote, append() appends to the end of the selected element, if you want to insert inside an element use $(selector).html(data); instead

0 comments:

Post a Comment