我正在使用XAMPP。 我正在使用json和ajax这个简单的实现。 它总是转到ajax的错误部分并显示这个异常错误:
AJAX http://localhost/myajax/employee/showAllEmployee 501(不是 实现)
的index.php:
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/bootstrap.min.css') ?>">
<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/css/bootstrap-theme.min.css') ?>">
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="<?php echo base_url(); ?>assets/js/bootstrap.min.js"></script>
<h2>Employee List</h2>
<div class="container">
<button class="btn btn-success">Add New</button>
<table class="table table-bordered table-responsive" style="margin-top: 20px">
<thead>
<tr>
<th>ID</th>
<th>Employee Name</th>
<th>Address</th>
<th>Created at</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Dara</td>
<td>Phnom Penh</td>
<td>2017</td>
<td>
<a href="javascript:;" class="btn btn-info">Edit</a>
<a href="javascript:;" class="btn btn-danger">Delete</a>
</tr>
</tbody>
</table>
</div>
<form>
</form>
脚本:
$(function(){
showAllEmployee();
function showAllEmployee(){
$.ajax({
type: 'ajax',
url: '<?php echo base_url() ?>employee/showAllEmployee',
async: false,
dataType: 'json',
success: function(data){
console.log(data);
alert('success');
},
error: function(){
alert('Could not get Data from Database');
}
});
}
});
</script>
控制器:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Employee extends CI_Controller {
function __construct() {
parent:: __construct();
$this->load->model('employee_m', 'm');
}
function index(){
$this->load->helper('url');
$this->load->view('employee/index');
}
public function showAllEmployee() {
$result = $this->m->showAllEmployee();
echo json_encode($result);
}
}
我尝试使用print_r()
检查数据库中是否确实存在数据,并且存在。
答案 0 :(得分:2)
更改
type: 'ajax',
要:
type: 'POST',
其他一切看起来都没问题