Welcome Guest, Not a member yet? Register   Sign In
ajax request with jquery problem

hi I'm having real problems getting jquery ajax requests to work, problem seem to be that the request doesn't go to the correct controller method and either returns with all the content for my 404 page or returns the content for the page you are already viewing, putting an extra / before the admin/process didn't help, have posted a simple example have tried a few ajax scripts that are working fine outside codeigniter and all seem to have the same problem any ideas very much appreciated

many thanks

this is the js file
  $("#submit_item").click(function() {
   var item = $('#itemid').val();
   $.post("admin/process", {'item' : item}, function(Data){

and this is the controller
function process() {
  $item= 'from controller'.trim($this->input->post('item'));
  $array = array('result'=>$item);
  echo $array;

Because you're using a relative URL. And if you're not using htaccess to process requests without index.php in them, you need to add that too.

yeah that makes sense, thanks !!

Replace $ with s in script tags.
<$cript type="text/javascript" charset="utf-8">
   var base_url = "&lt;?php echo base_url(); ?&gt;";
   var site_url = "&lt;?php echo site_url(); ?&gt;";
  // ]]>

Now you can use base_url and site_url in your jQuery.

$.post(base_url+"admin/process", {'item' : item}, function(Data){

Theme © iAndrew 2016 - Forum software by © MyBB