Tôi gặp sự cố với Codeigniter và JSON. Đây là mã của tôi:Codeigniter PHP JSON gửi phản hồi HTML
$.post("Admin/Admin/addschool", {test: 'test'}, function(data){
if (data.status == 'ok')
alert(data);
else
alert(data);
}, "json");
... và trong điều khiển của tôi:
public function addschool() {
$data = array("status" => "ok", "message"=> "something ");
echo json_encode($data);
exit();
}
Nhưng mỗi lần json tôi trả lời với mã HTML của toàn bộ quan điểm của tôi như phản ứng của tôi
<!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>
cố gắng để chạy chức năng này trong trình duyệt của bạn thanh url. Kiểm tra những gì nó trả về. Và cũng cố gắng sử dụng chức năng (dữ liệu, trạng thái, xhr). –
Thử '$ this-> output-> set_content_type ('application/json'); $ this-> output-> set_output (json_encode ($ data)); exit(); ' – GBD
Bạn có một số khung nhìn trong hàm tạo của controller không? – TheHorse