Csrf Ajax Codeigniter

Sy sedang mencoba menggunakan summernote. Saat memasukan gambar ke summernote, sy mau gambar tersebut otomatis masuk ke folder local dgn bantuan ajax. Kendalanya adalah karena sy mengaktifkan csrf, otomatis ajax gak bisa mengupload gambar yg di pilih. Saya sdh coba beberapa referensi yg kasusnya sama, tapi tetap belum bisa. Jujur saja, pengetahuan ajax sy masih minim. Berikut ini kode saya
Controller
//Upload image summernote
	function upload_image(){
		if(isset($_FILES["image"]["name"])){
			$config['upload_path'] = './assets/images/';
			$config['allowed_types'] = 'jpg|jpeg|png|gif';
			$this->upload->initialize($config);
			if(!$this->upload->do_upload('image')){
				$this->upload->display_errors();
				return FALSE;
			}else{
				$data = $this->upload->data();
				echo base_url().'assets/images/'.$data['file_name'];
			}
		}
	}

	//Delete image summernote
	function delete_image(){
		$src = $this->input->post('src');
		$file_name = str_replace(base_url(), '', $src);
		if(unlink($file_name)){
	        echo 'File Delete Successfully';
	    }
	}
view/form
<form action="" method="post">
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
				  <div class="form-group">
				    <label>Title</label>
				    <input type="text" name="title" class="form-control" placeholder="Title">
				    <small class="text-danger"><?php echo form_error('title'); ?></small>
				  </div>
				  <div class="form-group">
				    <label>Contents</label>
				    <textarea id="summernote" name="contents"></textarea>
				    <small class="text-danger"><?php echo form_error('contents'); ?></small>
				  </div>
				  <button type="submit" class="btn btn-primary">Submit</button>
				</form>
ajax
<script type="text/javascript">
		$(document).ready(function(){
			$('#summernote').summernote({
				height: "300px",
				callbacks: {
			        onImageUpload: function(image) {
			            uploadImage(image[0]);
			        },
			        onMediaDelete : function(target) {
			            deleteImage(target[0].src);
			        }
				}
			});

			function uploadImage(image) {
				var csrfName = '<?php echo $this->security->get_csrf_token_name(); ?>';
				var csrfHash = '<?php echo $this->security->get_csrf_hash(); ?>';
			    var data = new FormData();
			    data.append("image", image);
			    $.ajax({
			        url: "<?php echo site_url('post/upload_image')?>",
			        cache: false,
			        contentType: false,
			        processData: false,
			        data: data,
			        type: "POST",
			        typeData: 'json',
			        data: {csrfName: csrfHash},
			        success: function(url) {
						$('#summernote').summernote("insertImage", url);
			        },
			        error: function(data) {
			            console.log(data);
			        }
			    });
			}

			function deleteImage(src) {
			    $.ajax({
			        data: {src : src},
			        type: "POST",
			        url: "<?php echo site_url('post/delete_image')?>",
			        cache: false,
			        success: function(response) {
			            console.log(response);
			        }
			    });
			}

		});
		
	</script>
Mungkin teman2 bisa bantu untuk kasus sy ini. Terima kasih
avatar Ahmad554

Ditanyakan oleh @Ahmad554

52 Kontribusi 19 Poin

Login untuk menanggapi


Jawaban

Belum ada Jawaban, jadi yang pertama

Login untuk gabung berdiskusi