[英]displaying codeigniter database error using ajax
我收到錯誤500內部服務器錯誤
我想顯示數據庫錯誤以供查看,到目前為止,我只能在chrome網絡響應中生成響應
模型類,我省略了構造函數
class Size_mdl extends MY_Model {
public function create($data) {
return $this->insert($data);
}
}
class MY_Model extends CI_Model {
public function insert($data) {
$success = $this->db->insert($this->table_name, $data);
if ($success) {
return true;
} else {
$msg = $this->db->error();
return $msg;
}
}
控制者
$data['test_r'] = var_dump($this->Size_mdl->create($data));
$this->load->view('size/create_size_view',$data);
視圖
這是Welcome.php,最外面是頁眉和頁腳
<div class="page">
<!-- Page Header-->
<header class="page-head"></header>
<!-- Page Content-->
<main id="main_content" class="page-content"></main>
<!-- Page Footer-->
<footer class="page-foot section-60 section-xl-150 text-center text-lg-left">
在該頁面內,我已將create_size_view加載到main_content,以前是主頁,一堆東西。
<head>
<style>
</style>
</head>
<body>
<div id="container">
<div id="content">
<h5>Create New Size</h5>
<div id="inner_container">
<div id="create_part">
<?php
if (isset($test_r)){
echo $test_r;
}?>
<?php echo validation_errors(); ?>
<?php
$attributes = array('id' => 'create_size_form');
echo form_open("size/create", $attributes);
?>
<p>
<?php echo lang('size_short_name_label', 'short_name'); ?>
<?php
$short_name = array(
'name' => 'short_name',
'id' => 'short_name',
'maxlength' => '100'
);
echo form_input($short_name);?>
</p>
<p>
<?php echo lang('size_long_name_label', 'long_name'); ?>
<?php
$long_name = array(
'name' => 'long_name',
'id' => 'long_name',
'maxlength' => '100',
);
echo form_input($long_name);?>
</p>
<p>
<?php echo form_submit('submit', lang('create_size_submit_btn')); ?>
</p>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
<script>
$('form#create_size_form').submit(function (e) {
var form = $(this);
e.preventDefault();
$.ajax({
type: "POST",
url: "<?php echo site_url('size/create'); ?>",
data: form.serialize(), // <--- THIS IS THE CHANGE
dataType: "html",
success: function (result) {
$('#main_content').html(result);
},
});
});
</script>
</body>
現在,我需要響應來解決插入失敗的問題,該問題似乎取代了create_size_view.php中的main_content welcome.php或其子級div。 我可以通過重新加載create_size_view.php來產生成功的插入
更改ajax url屬性。 使用“ / size / create”之類的網址,不要使用“ www.site.com/size/create”之類的域名;
使用文本綁定數據
$.ajax({
type: "POST",
url: "<?php echo site_url('size/create'); ?>",
data: form.serialize(), // <--- THIS IS THE CHANGE
dataType: "html",
success: function (result) {
$('#main_content').text(result);
}
});
在Codeigniter中,針對MySQL的預定義錯誤處理程序。 如果遇到錯誤,Codeigniter會拋出HTML內容,並帶有500個服務器站點的錯誤,以避免出現此頁面500狀態代碼將Codeigniter ENVIRONMENT投入生產,那么您的代碼將正常運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.