【发布时间】:2017-02-21 15:17:22
【问题描述】:
我正在使用 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() 来检查数据库中是否真的有数据,并且有。
【问题讨论】:
-
如果您从浏览器位置栏调用服务器端代码,我们是否应该假设它可以正常工作?
标签: php jquery json ajax codeigniter