Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter PHP JSON sends HTML response

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>
like image 900
MPrinsloo Avatar asked Oct 05 '22 16:10

MPrinsloo


1 Answers

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

like image 126
Fortran Avatar answered Oct 08 '22 20:10

Fortran