So I am using a jQuery .load() call to implement endless pagination in a CakePHP web app.
Here is the Javascript code:
$("#post_container").append('<div class="batch row" style="display:none;"></div>'); //append a container to hold ajax content
var url = $("a#next").attr("href"); //extract the URL from the "next" link
$(".paging").remove(); // remove the old pagination links because new ones will be loaded via ajax
if(url === undefined)
{
$("#ajax_pagination_text").html("<strong>Turtles all the way down. No more posts :(</strong>");
}
$("div.batch").load(url, function(response, status, xhr) {
if (status == "error") {
var msg = "Sorry but there was an error: ";
alert(msg + xhr.status + " " + xhr.statusText);
}
else {
$(this).attr("class","loaded row"); //change the class name so it will not be confused with the next batch
$(".paging").hide(); //hide the new paging links
$(this).fadeIn();
}
});
The important part here is the $.load() which tries to load a url (http://www.MYWEBSITE.com/posts/index/page:2). When jQuery makes the request the server returns a 500 internal server error Same thing if I use jQuery.ajax(url);. If I go to that url directly it loads, no problem.
The ajax call works fine on my local development server as well. So this must be something with the setup on my host 1and1. What’s going on here? I’ve already had to deal with a few 500 server errors with this host.
I’ve changed my .htaccess files as follows to fix previous 500 errors:
.htaccess in CakePHP root:
<IfModule mod_rewrite.c>
AddType x-mapp-php5 .php
SetEnv PHP_VER
RewriteEngine ON
RewriteBase /
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
.htaccess in CakePHP app/:
<IfModule mod_rewrite.c>
AddType x-mapp-php5 .php
RewriteEngine ON
RewriteBase /
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
.htaccess in CakePHP app/webroot/:
<IfModule mod_rewrite.c>
AddType x-mapp-php5 .php
AddHandler x-mapp-php5 .php
RewriteEngine ON
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?q=$1 [QSA,L]
</IfModule>
any help or insight is much appreciated!
So it turns out this was a problem in the way I was changing which view was rendered by Cake depending on whether the call was AJAX or not.
Originally I had this, to change the view that is rendered:
Which worked flawlessly on my local development server. But once uploaded this was having problems. The server said it couldn’t find the view.
Changing it to the following fixed the issue:
I’m guessing it had to do with the fact that the actual folder “Posts” has a capitol P and my windows server is not case sensitive so “/posts” resolved to “/Posts” but once I put it on the linux servers at 1and1 the case became an issue.. Also after reading the CakePHP documentation I realized I didn’t even need the directory since the view was from the same controller. Hence the resulting code.