我正在尝试创建一个评级星级来评估商店,我使用了ChatGPT,显然代码是正确的,但是每当我单击发送按钮(提交)时,它都会在控制台中显示404错误。我不知道我错在哪里
<!-- Rating Section Start -->
<div class="container">
<h2>Rating System</h2>
<!-- Rating Form -->
<form id="ratingForm">
<div class="form-group">
<label for="user_name">Your Name:</label>
<input type="text" class="form-control" id="user_name" name="user_name" required>
</div>
<div class="form-group">
<label for="user_rating">Your Rating:</label>
<select class="form-control" id="user_rating" name="user_rating" required>
<option value="1">1 Star</option>
<option value="2">2 Stars</option>
<option value="3">3 Stars</option>
<option value="4">4 Stars</option>
<option value="5">5 Stars</option>
</select>
</div>
<div class="form-group">
<label for="user_comment">Your Comment:</label>
<textarea class="form-control" id="user_comment" name="user_comment" rows="3" required></textarea>
</div>
<button type="button" class="btn btn-primary" onclick="submitRating()">Submit Rating</button>
</form>
<hr>
<!-- Display Ratings -->
<h3>Recent Reviews:</h3>
<div id="reviews"></div>
</div>
<!-- Rating Section End -->
<script>
// Submit Rating via Ajax
function submitRating() {
var formData = $('#ratingForm').serialize();
$.ajax({
type: 'POST',
url: '<?php echo base_url('ReviewController/submit_rating'); ?>',
data: formData,
dataType: 'json',
success: function (response) {
alert(response.message);
loadReviews();
$('#ratingForm')[0].reset();
},
error: function (error) {
console.log(error);
}
});
}
// Load Reviews via Ajax
function loadReviews() {
$.ajax({
type: 'GET',
url: '<?php echo base_url('ReviewController/load_reviews'); ?>',
dataType: 'json',
success: function (reviews) {
displayReviews(reviews);
},
error: function (error) {
console.log(error);
}
});
}
// Display Reviews
function displayReviews(reviews) {
var html = '';
$.each(reviews, function (index, review) {
html += '<div class="panel panel-default">';
html += '<div class="panel-heading"><strong>' + review.user_name + '</strong> rated ' + review.user_rating + ' stars</div>';
html += '<div class="panel-body">' + review.user_comment + '</div>';
html += '</div>';
});
$('#reviews').html(html);
}
// Initial Load of Reviews
$(document).ready(function () {
loadReviews();
});
</script>
//controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ReviewController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('ReviewModel');
}
public function index() {
$this->load->view('pages/home');
}
public function submit_review() {
$data = array(
'user_name' => $this->input->post('user_name'),
'user_rating' => $this->input->post('user_rating'),
'user_comment' => $this->input->post('user_comment')
);
$this->ReviewModel->insert_review($data);
echo json_encode(array('message' => 'Review submitted successfully.'));
}
public function load_reviews() {
$reviews = $this->ReviewModel->get_reviews();
echo json_encode($reviews);
}
}
//model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class ReviewModel extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function insert_review($data) {
$this->db->insert('reviews', $data);
}
public function get_reviews() {
$this->db->order_by('created_at', 'DESC');
$query = $this->db->get('reviews');
return $query->result();
}
}
//routes.php
<?php
defined('BASEPATH') or exit('No direct script access allowed');
$route['default_controller'] = 'ReviewController';
$route['submit-review'] = 'ReviewController/submit_review';
$route['404_override'] = '';
$route['translate_uri_dashes'] = true;
//config.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
| WARNING: You MUST set this value!
|
| If it is not set, then CodeIgniter will try to guess the protocol and
| path to your installation, but due to security concerns the hostname will
| be set to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/
$config['base_url'] = 'http://localhost/store-nike/';
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';
我想帮助确定我哪里出错了,所以代码结构良好并且有正确的路线
$.ajax({
类型:“发布”,
网址: '',
数据:表单数据,
数据类型:'json',
成功:功能(响应){
警报(响应.消息);
加载评论();
$('# ratingForm')[0].reset();
},
错误:函数(错误){
控制台.log(错误);
}
});