hi i’m building one basic website from codeigniter, i’m basically laravel developer but for some reason had to get look with codeigniter, here i’m making an ajax call with the codeigniter view page, and i’m following https://makitweb.com/send-ajax-request-codeigniter/ this tutorial,
and here is i tried code
$.ajax({
type: "POST",
url: "<?php echo base_url('messages/msgnotification'); ?>",
data: "userid=" + '<?php echo $this->uri->segment(2); ?>',
success: function(msg){
console.log(msg);
}
});
and this is in controller
class Messages extends CI_Controller {
function __construct() {
Parent::__construct();
$this->load->model('Chat_notification_model');
$getSession = $this->session->userdata;
public function index() {
}
// get unread chat
function msgnotification(){
// POST data
$postData = $this->input->post();
echo $postData;
// get data
$data = $this->Chat_notification_model->get_chat_notification($postData);
echo json_encode($data);
}
and here is my modal
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Chat_notification_model extends CI_Model {
function __construct() {
Parent::__construct();
}
function get_chat_notification($postData=array()){
$response = array();
if(isset($postData['user_id']) ){
// Select record
$this->db->select('count(*)');
$this->db->where('msg_id', $postData['user_id']);
$records = $this->db->get('Messages');
$cnt = $records->row_array();
$response = $cnt['count(*)'];
}
return $response;
}
?>
i referred below SO links
- Codeigniter ajax call 404 error on hosting
- Codeigniter – Error on Ajax call (404)
- Codeigniter Ajax gets 404 Error
but none are helped me from above, any helps are thank you
2
Answers
The mistake I was done here is I didn't made setup the my routing.php to accept the url I'm passing from the ajax request.
This may help some who are searching for this.
and remaining part will remained as same in question.
I got stuck only because the tutorial I followed didn't use the routing part.
You have passed wrong data with post method in ajax
your code:
data: "userid=" + '<?php echo $this->uri->segment(2); ?>',
Need to update or replace the code:
data: { "userid :" + '<?php echo $this->uri->segment(2); ?>' },
Let me know If you till getting 404 not found.