0

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.

3
  • What error you got while posting your data. Have you check your browser console???? Commented Jul 31, 2015 at 8:16
  • There is no error :/ and console log empty Commented Jul 31, 2015 at 8:19
  • try with $arr = array('done'); echo json_encode($arr); Commented Jul 31, 2015 at 8:24

3 Answers 3

0

Try with this one

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"); 

      }
});

}
Sign up to request clarification or add additional context in comments.

6 Comments

What error you got while posting your data ? Would you please check console log to make sure that there is no error ? Do you have any other JS error in that page from other 3rd party script error?
yes actually i have an error log like this : Uncaught TypeError: $(...).jcarousel is not a function and this is that errors script : <script src="../js/jquery.jcarousel.pack.js" type="text/javascript"></script>
I think you have a jcarousel in home page or any other page except this page, if this is true then don't load jacrousel related stuffs in this page. Therefore if you are using jcarousel in this page also then it is not proper initialized. please check this
I remove jcarousel and still ajax not work :/ I dont know why this happened. I was made it successfully with pure php (without codeigniter) .
Please make sure another thing that you don't have any trailing slash in your base url
|
0

Post data should be like below one

data: { name: "John", location: "Boston" }

Not like

  data:  "kAdi="+kAdi,

Also Print the URl and check it access or not

1 Comment

Thanks but not worked. Also i was use this "kAdi="+kAdi syntax with pure php . But i think the issue is not that.
0

Use this

    $.post("<?php echo base_url() ?>main/login",
    {
       kAdi: kAdi                
    },
    function(cevap){
    console.log(cevap.trim());
    if(cevap){
     alert(cevap);
     alert("sucessful");
    }
    });

On the other hand in controller,use this

$this->input->post('kAdi');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.