2

I'm using Jquery ajax to call a CodeIgniter function:

$.ajax({
  type: "POST",
  dataType:'json',
  beforeSend: function() {
  $("#opc").addClass("opacity");
  $("#searching").show(); 
  },
  url: "<?php echo base_url(); ?>search/get_results",
  data: {
    'ns_pos':ns_pos,
    'NSPlaceDomainID':activity,
    'DistrictID':area,
    'NSAssociationID':referer,
    'Title':text,
    'SettlementID':settlement,
    'NoOpinion':$("input[name=NoOpinion]").is(":checked"),
     'SpecialCharacteristics':ns_attr
  } 

etc...

In the Chrome console I get the message:

POST http://mattat.org.il/ci/index.php/search/get_results 404 (Not Found) 

the response preview is:

Not Found

The requested URL /ci/index.php/search/get_results was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

However, this function does exist, as is evident when I simply paste the link in the URL line.

I'm confused...

edit: Following @paul's constructive comment I have changed the request to GET and, indeed, the page was found. What can cause such an obscure behaviour?

One more step: When I empty the data object (like so: data: {}), the function is found even with POST request

Matanya
  • 6,233
  • 9
  • 47
  • 80
  • 1
    Are you making this call across domains? – Mike Park Oct 16 '12 at 20:32
  • no, It's being called from within: http://mattat.org.il/ci/index.php/search/search_ns – Matanya Oct 16 '12 at 20:33
  • The page is returning a 500 Database Error for me... – A.M.K Oct 16 '12 at 20:35
  • I know, but that doesn't concern me, because the function should be passed arguments. What baffles me is that it can find it when you reference it directly, but not when I call it via AJAX – Matanya Oct 16 '12 at 20:37
  • Have you tried running fiddler (or something else) to make sure the traffic you expect is happening? – CaffGeek Oct 16 '12 at 20:42
  • 4
    Could be an issue of post vs get. When you type the URL into the browser you are making a get request. Your Ajax is making a post request. – Paul Oct 16 '12 at 20:42

3 Answers3

2

As Paul already pointed, it is likely an error due to GET/POST limitation. Check your server code if you support POST requests on this endpoint.

Eugene Y.
  • 196
  • 6
  • Indeed it is. However, I have plenty other AJAX requests on the same page, which are calling different functions from the same controller, and work flawlessly. – Matanya Oct 16 '12 at 20:52
0

Instead of using base_url() please use site_url(). I have experienced the same issue.

A simple example could be given as follows in which an ajax jQuery posting is viewed.

 $.post("<?php echo site_url('message/add') ?>", {message: msg}, function() {
        $('#content').load("<?php echo site_url('message/view/ajax') ?>");
        $('#message').val('');
    });

Message/add here as you can guess the function is defined in controller. As I firstly deal with php, I searched for a good example. I am not only news at php but also at codeigniter. In many example, the implemented form is like "base_url+message/add". It might be possible that codeigniter version leads to this error since all of them were written by the version prior to 2.X. Anyway, if you want to have a ready code please check the following link, it worked for me!

jQuery ajax codeigniter example

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

This same issue had me baffled. It turned out to be an Apache permissions problem. I checked with my hosting provider and they confirmed, fixed it and showed me how to setup a Cron job that runs every hour to reassert proper permissions. They also suggested I use FileZila rather than WinSCP and provided me with the settings to automatically reset permissions on reuploading files.

ron tornambe
  • 10,452
  • 7
  • 33
  • 60