I have a problem with ajax post to codeigniter controller. It is get in to function but not post the data. Why ajax is not successfully post data ?
Here is my controller :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller{
public function index(){
$this->load->helper('url');
$this->load->view('index');
}
public function login(){
if ($_POST) {
$kAdi = $this->input->post('kAdi');
echo json_encode("done!");
}
}
}?>
my Ajax code :
function girisYap(){
var kAdi = $("#username").val();
var parola = $("#password").val();
$.ajax({
type:"POST",
url: "<?php echo base_url(); ?>" + "/main/login",
data: "kAdi="+kAdi,
dataType: 'json',
success : function(cevap){
alert("successfull");
}
});
}
Am I missing something ?
I solve the porblem, thank you guys. Here is the works code :
The controller
public function login(){
if ($_POST) {
$kAdi = $this->input->post('kAdi');
echo json_encode("done!");
}
Ajax function :
function girisYap(){
var $_base_url = '<?=base_url()?>';
var kAdi = $("#username").val();
var parola = $("#password").val();
alert($_base_url+"main/login");
$.ajax({
type:"POST",
url: $_base_url + "main/login",
data: {kAdi: kAdi},
dataType: 'json',
success : function(cevap){
alert(cevap);
}
});
}
Don't put the / before main/login because base url = localhost/codeigniter/ has a slash in the end. If you put slash it become codeigniter//main....
So, thanks for all the answers.
$arr = array('done'); echo json_encode($arr);