CodeIgniter Forums
Ajax callback fails (but used to work) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Ajax callback fails (but used to work) (/showthread.php?tid=50071)



Ajax callback fails (but used to work) - El Forum - 03-13-2012

[eluser]Ivoo[/eluser]
OK, this is weird. I have an jQuery ajax call-back to my CodeIgniter controller. A month ago, this worked fine. But now it stopped working. The problem is that the controller-function is not reached.

Also, it is not a routing problem. When I manually point my browser to the my callback function (repository/asm), it works fine.

I suspect that I unknowingly changed some configuration, or some setting. But what?

Any suggestions?

jQuery:
Code:
$.ajax({
   type: 'POST',
   url: "repository/asm",
   async: false,
   data: { mediaID: mediaID },
   success: function(jsonData){
    data = $.parseJSON(jsonData);
    $('#innerImageFrame img').attr("src",data.uri);
    $('#titleFrame').html(data.titleInArchive);
    $('#commentFrame').html(data.commentInArchive);
    $('#yearselect').val(data.yearselect);
    $('#monthselect').val(data.monthselect);
    $('#dayselect').val(data.dayselect);
    $('#starRating').raty('start', data.starRating);
   }
  });


CodeIgniter:
Code:
<?php // if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Repository extends CI_Controller {

public function __construct()
{
  
  parent::__construct();
  $this->load->helper('url');
  $this->load->model('ifm_media_model');
  // $this->load->model('news_model');
}


  public function asm()
  {
  log_message('debug', '*** asm (ajax callback) (repository.php) was called');

// .. other code, but the above message is already never logged



Ajax callback fails (but used to work) - El Forum - 03-13-2012

[eluser]InsiteFX[/eluser]
Try:
Code:
url: <?php echo base_url()?>+"repository/asm",



Ajax callback fails (but used to work) - El Forum - 03-13-2012

[eluser]Ivoo[/eluser]
Thanks for looking at this. I did not know you could create php-islands in jQuery like you can in HTML. Thanks for showing that.

Sadly, it does not resolve the problem. Further investigations taught me that the post-request results in

"NetworkError: 500 Internal Server Error - http://www.essential-strategy.com/repository/repository/asm"

I am now trying to get support from my hoster (Dreamhost). They are usually good with support, so hopefully that will point me in the right direction.




Ajax callback fails (but used to work) - El Forum - 03-13-2012

[eluser]Ivoo[/eluser]
resolved!

It turns out that this problem (ajax callback results in 500 internal server error) can be caused by the

|--------------------------------------------------------------------------
| Cross Site Request Forgery
|--------------------------------------------------------------------------
| Enables a CSRF cookie token to be set.

The problem can be worked around by setting:

Code:
$config['csrf_protection'] = FALSE;

in CodeIgniter's /application/config/config.php file.

Don't ask me why, because I do not know. I just found an off-hand remark that pointed me in the right direction here.


Ajax callback fails (but used to work) - El Forum - 03-13-2012

[eluser]CroNiX[/eluser]
Or, you can send the csrf token along with your form...