I have data that I want to send from a view to a controller. And I'm trying to use fetch to send it. The data has been successfully sent with a status of 200 and the data is in the request payload on the network. But when I want to try to display it in the data controller it outputs null. Does anyone have a solution to make the data is not null in the controller?
This is my script in view:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<button id="kirimkecontroller">Kirim</button>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
<script>
// var data = {
// 'testing': 'isi testing'
// }
let formData = new FormData()
formData.append('testing', 'isi testing')
const button = document.querySelector('#kirimkecontroller')
button.addEventListener('click', () => {
fetch("<?= base_url(); ?>/welcome/kiriman_dari_fetch", {
method: "POST",
mode: 'no-cors',
headers: {
"Content-Type": "application/json"
},
body: formData,
}).then(res => console.log('sukses ', res.status))
.catch(e => console.log('gagal ', e));
window.open("<?php echo base_url(); ?>welcome/kiriman_dari_fetch", '_blank');
})
</script>
</body>
</html>
And This is my script in controller:
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Welcome extends CI_Controller
{
public function index()
{
$this->load->view('welcome_message');
}
public function kiriman_dari_fetch()
{
$testing = $this->input->post('testing');
echo json_encode($testing); // null
}
}
And output is null
In Inspect Element > Nework:
How do I get the data from there to the controller?