CodeIgniter/jQuery - Ajax call returns full html page instead of my echo

14,411

Solution 1

I solved this by using a leading slash as suggested in the comments to my question.

$.ajax({
  type: "POST",
  url: "/planner/get_cal",
  dataType: "text",
  data: {current_month: current_month},
  success: function(msg){
    alert(msg);
  } 
});         

Solution 2

You can also get it by adding exit after echo in your php file like below:

function get_cal()
 {
    echo "dinosaurs";exit;
}

It will work. :)

Share:
14,411

Related videos on Youtube

Joris Ooms
Author by

Joris Ooms

Updated on June 04, 2022

Comments

  • Joris Ooms
    Joris Ooms almost 2 years

    In my view I have an ajax call:

        $(".previous").click(function() {
            $.ajax({
                type: "POST",
                url: "planner/get_cal",
                data: {current_month: current_month},
                success: function(msg){
                    alert(msg);
                }
    
            });
    

    the get_cal function in my Planner controller:

    function get_cal()
    {
        echo "dinosaurs";
    }
    

    However, instead of returning "dinosaurs", it returns a full HTML page. I can't figure out why. Thoughts? Thanks a lot.

    • sleepisforthewebless
      sleepisforthewebless about 13 years
      Have you tried adding a leading slash? url: "/planner/get_cal",
    • David Houde
      David Houde about 13 years
      I second the leading / -- Otherwise things look just fine, and this shouldn't be happening.
  • Scaramouche
    Scaramouche over 5 years
    this works right now. I changed exit for return though, :)

Related