Ajax jQuery post to PHP script gives error 404

advertisements

I'm trying to program an online experiment which posts some data using jQuery and ajax to a PHP script which saves the data to the text file, and I'm having some issues. The script works fine when the data I post is small (say 2kb) and will correctly save the posted data, but when the data in the post becomes 4kb or larger, I get a 404 error instead, and the PHP script does not run at all (no data is saved to file), which is very odd. Here's the relevant javascript and PHP code:

experiment.js:

$.ajax({
    url: 'snippets/saveData.php',
    data: {"time": time, "experimentStimuli": experimentStimuli, "experimentPredictions": experimentPredictions, "experimentPredictionsEnd": experimentPredictionsEnd},
    type: 'POST'
    });

saveData.php:

$_SESSION['user']['experimentStimuli'] = $_POST['experimentStimuli'];
$_SESSION['user']['experimentPredictions'] = $_POST['experimentPredictions'];
$_SESSION['user']['experimentPredictionsEnd'] = $_POST['experimentPredictionsEnd'];

foreach($_SESSION['user']['experimentStimuli'] as $key => $value) {
  foreach($value as $key2 => $value2) {
    fwrite($fh, $value2);
  }
  fwrite($fh, "\n");
}

foreach($_SESSION['user']['experimentPredictions'] as $key => $value) {
  foreach($value as $key2 => $value2) {
    fwrite($fh, $value2);
  }
  fwrite($fh, "\n");
}

foreach($_SESSION['user']['experimentPredictionsEnd'] as $key => $value) {
  foreach($value as $key2 => $value2) {
    fwrite($fh, $value2);
  }
  fwrite($fh, "\n");
}

I've tried using an absolute URL instead of a relative one but it makes no difference (the relative URL works fine for small data), also I've tried putting header('HTTP/1.1 200 OK'); at the top of my PHP script but this doesn't seem to work either. While the base of my website is Wordpress, my experiment code is separate from it and does not require any Wordpress functions, but the 404 error page it displays is the Wordpress 404 page. I have a feeling that Wordpress may be interfering somehow (also because it works fine locally), but I'm not sure how to fix it. Any ideas?


Ajax in wordpress is handled a bit differently , Please read here : http://codex.wordpress.org/AJAX_in_Plugins This article will explain in an easy was how to use AJAX in wordpress, both for the Admin - End and also for the User - end . Even if it is called "ajax in plugins " it actually describes the easiest way to handle ajax in wordpress.