5

I have a problem with Codeigniter and JSON. Here is my coding:

$.post("Admin/Admin/addschool", {test: 'test'}, function(data){             
  if (data.status == 'ok')
    alert(data);
  else 
    alert(data);
}, "json");

... and in my controller:

public function addschool() {
  $data = array("status" => "ok", "message"=> "something ");  
  echo json_encode($data);  
  exit(); 
}

But each time my json reply with the HTML of my whole view e.g my response

<!doctype html>

<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://localhost:10090/css/layout.css" />
<title>Administration</title>
<meta name="description" content="">
<meta name="author" content="">
</head>
4
  • 2
    try to run this function in your browser url bar. Check what it returns. And also try to use function(data,status,xhr). Commented Dec 13, 2012 at 10:56
  • 2
    Try $this->output->set_content_type('application/json'); $this->output->set_output(json_encode($data));exit(); Commented Dec 13, 2012 at 11:06
  • Do you have some views in controller's constructor? Commented Dec 13, 2012 at 11:11
  • The file that contains the ajax call, what is it's path ? You may want /Admin/Admin/addschool instead. Commented Dec 13, 2012 at 21:05

3 Answers 3

2

First, it is good practice not to you used "echo", and you use the 'return'. Try to put down the controller directly to url and see if your return json

Sign up to request clarification or add additional context in comments.

1 Comment

He's not returning anything though, he's echoing it out to the browser.
1

Try use absolute URL

$.post("/Admin/Admin/addschool", {test: 'test'}, function(data){             
  if (data.status == 'ok')
    alert(data);
  else 
    alert(data);
}, "json");

And it is better return then exit inside codeigniter function

public function addschool() {
  $data = array("status" => "ok", "message"=> "something ");  
  echo json_encode($data);  
  return; 
}

1 Comment

codeigniter is automatically send html (layout) for every requests. he want to disable that thing, not what your answer is.
1

Probably there is a problem with the url. Let me give u an example of url routing problem

Lets say, the admin controller index function loads the admin home page, so in the load method, in the url part, if u just write "addschool", the ajax call will go to addschool function.

A chaining is done automatically, admin/addschool

But say for example, the function admin/load_view loads ur view page, now if u type addschool in the url, chaining will take place, and the url will become

admin/load_view/addschool

So you have to check the codeigniter function that is loading the view page, then use the appropriate chaining

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.