0

I send and ajax request to the server and I want to receive json response but instead, I receive html response, what is the wrong in this code?

//jquery code

$('select[name=category]').click(function(){
    $.ajax({
    url: "/index.php/category/get_categories",
    type: "post",  
    dataType: "json",
    cache: false,      
    success: function (result) { 
        var arr = jquery.parseJSON(result);
        alert(arr);
        }      
    });
});

//php code

public function get_categories(){
    $data = $this->category_model->get_cats_names_ids();
    echo json_encode($data);
}

the response is an html page instead of json object and the alert box dose not appear. when I remove dataType:"json", the alert box appear and contain html page ! and any code after "var arr = jquery.parseJSON(result);" does not work, ex. alert("hi"); !

4
  • what does the html page contain? Commented Dec 4, 2012 at 2:53
  • @ekims : the html page is the WAMPSERVER Homepage !! Commented Dec 4, 2012 at 11:37
  • You do not need to use parseJSON(result): if you fetched with type: "json", the result should already be parsed. Commented Dec 5, 2012 at 0:05
  • @itachi I have been debugging my crazy HTML response instead of JSON for days now... and this simple suggestion helped me figure it all out. THANK YOU. Commented Nov 23, 2015 at 14:45

3 Answers 3

3

I don't know if this will completely resolve your problem (there may be a display hook or other view machinery involved which is generating html), but start here.

Rule 1: Never echo anything from your controller. Either invoke a view or use output::set_output.

Rule 2: Always set your content-type correctly.

public function get_categories(){
    $data = $this->category_model->get_cats_names_ids();
    $this->output->set_header('Content-type:application/json');
    $this->output->set_output(json_encode($data));
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this code, but response still an html page "WAMPSERVER Homepage". and there is no error at the model, I invoke the function by url and simply echo the data !
0

It seems that there is something error in your model and the HTML response you got is CI error message.

Just for debugging, echo the $data without json_encode and then call that function directly via URL.

Comments

0

try following

$('select[name=category]').click(function()
{
 $.post( '<?php echo site_url('category/get_categories'); ?>', { 'var': 1 },
       function(response) {
       if(response.success)
    {
         var arr = response.message;
                     alert(arr);
    }       
      },"json");


});

 public function get_categories()
 {
    $data = $this->category_model->get_cats_names_ids();
    echo json_encode(array('success'=>true,'message'=>$data));
    //for testing replace above line with
    // echo json_encode(array('success'=>true,'message'=>'Hello!'));
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.