admin管理员组

文章数量:1330679

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

               });

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

               });
Share Improve this question edited Sep 26, 2012 at 1:57 John Conde 220k99 gold badges462 silver badges501 bronze badges asked Jun 30, 2011 at 2:40 kooolkoool 15.5k11 gold badges46 silver badges60 bronze badges 2
  • 2 Can you elaborate please? Your question is very vague. Generally 'not working' is not a good explanation. – Mrchief 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 – user745235 Commented Jun 30, 2011 at 20:35
Add a ment  | 

4 Answers 4

Reset to default 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);
     }
});

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 remend using Firebug to look at the activity going on under the hood: it might make pinpointing these errors easier for you.

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;

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

        }
    });

本文标签: javascriptcalling a codeigniter controller method from jqueryStack Overflow