Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 517
Joris Ooms Avatar asked Apr 12 '11 15:04

Joris Ooms


2 Answers

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);
  } 
});         
like image 90
Joris Ooms Avatar answered Oct 22 '22 09:10

Joris Ooms


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. :)

like image 36
RiksAndroid Avatar answered Oct 22 '22 08:10

RiksAndroid