3

I wanted to call a codeigniter controller method from within jquery as of now I am trying something like this but it isn't working could anyone help me out with this

$.ajax({type: "POST", url:"site/controller/method/",

                  success: function(){
                      alert("success");
                  },

                  error: function(xhr, textStatus, error){
                              alert(xhr.statusText);
                              alert(textStatus);
                              alert(error);
                          }

               });
2
  • 2
    Can you elaborate please? Your question is very vague. Generally 'not working' is not a good explanation. Commented Jun 30, 2011 at 2:44
  • 1
    you need to set the full path on your url, like: http:localhost/site/controller/method/, I use it this way and works properly and you can pass variables with the 'data:' or in your URL Commented Jun 30, 2011 at 20:35

4 Answers 4

3

I myself use CI, and have found that I need to point directly to the file when processing AJAX requests, and if needed, push some variables with it.

$.ajax({
    type: "POST",
    url: "system/application/views/ajax.php",
    data: "key=value",
    success: function(){ alert("success"); },
    error: function(xhr, textStatus, error){
        alert(xhr.statusText);
        alert(textStatus);
        alert(error);
     }
});
Sign up to request clarification or add additional context in comments.

3 Comments

@D Franks thanks for the answer but how do I just call a particular method within a controller, By the above procedure I would be able to reach the controller but not the method within a controller and I hope there is a way to address this problem
Although I don't think there is a way to do this directly (or I'm unaware of), you could either add an if statement to run your method, e.g.: if($_POST['call-from-ajax']){ run_method(); }, or you could look into this I found in the CI wiki: codeigniter.com/wiki/AJAX_for_CodeIgniter
@ D Franks, you can use site_url() in place of the system/application part. $.ajax({ url: "<?php echo site_url('forms/textup'); ?>",
1

Your question is very vague: did you get a 404? Did you get a javascript error? What was the problem?

If you are having 404 errors the easiest way to make this work is to test out the URL in your browser's location bar: any URL you can open in the browser location bar can be copied into the javascript and will definitely work. There should be no difference on the code igniter side between URLs that are called from the browser location bar or from an anchor and URLs that are called from a jquery post or get or ajax.

I'd recommend using Firebug to look at the activity going on under the hood: it might make pinpointing these errors easier for you.

1 Comment

I used mozilla firefox web console and usually it shows the post request, but, i this case the problem is there is no post request shown and there is no error
0

I made a controller called ajax, where i have all my axaj methods. And return stuff json encoded.

jquery stuff:

    $.post("ajax/stuff", function(data){
    alert(data.some_data); // John
 }, "json");

in ajax controller:

public function stuff()
{
    $response_array = array();
    $response_array['some_data'] = 'lorem ipsum';
    //$response_array['html'] = $this->load->view('popbox_error', $data, true);

    $data['json'] = json_encode($response_array);
    $this->load->view('json_response', $data);
}

json view:

<?php echo $json;

1 Comment

can explain further on $.post("ajax/stuff" . i dont really know what should i put there. im new in ajax. :) thanks in advance
0

Try this

var base_url ='your base url;

$.post(base_url+"/controller/function", {'param1': x,'param2':y}, function(data){

        if(data.length >0) {

            //do whatever you want

        }
    });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.