doublesecretagency / craft-starratings

Star Ratings plugin for Craft CMS
Other
4 stars 3 forks source link

Ajax error 400 #8

Closed LouisCuvelier closed 5 years ago

LouisCuvelier commented 5 years ago

Hi,

I bought the plugin but It doesn't work as expected. When I click on a star to set the rating, It show an error 400. Here's the error :

yii\web\BadRequestHttpException: Post request required in /home/coursier/craft/vendor/craftcms/cms/src/web/Controller.php:251
Stack trace:
#0 /home/coursier/craft/vendor/doublesecretagency/craft-starratings/src/controllers/RateController.php(38): craft\web\Controller->requirePostRequest()
#1 [internal function]: doublesecretagency\starratings\controllers\RateController->actionIndex()
#2 /home/coursier/craft/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#3 /home/coursier/craft/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#4 /home/coursier/craft/vendor/craftcms/cms/src/web/Controller.php(109): yii\base\Controller->runAction('', Array)
#5 /home/coursier/craft/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('', Array)
#6 /home/coursier/craft/vendor/craftcms/cms/src/web/Application.php(297): yii\base\Module->runAction('star-ratings/ra...', Array)
#7 /home/coursier/craft/vendor/craftcms/cms/src/web/Application.php(561): craft\web\Application->runAction('star-ratings/ra...', Array)
#8 /home/coursier/craft/vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#9 /home/coursier/craft/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#10 /home/coursier/public_html/index.php(21): yii\base\Application->run()
#11 {main}

How can I fix this please ?

Regards, Louis

lindseydiloreto commented 5 years ago

Hi @LouisCuvelier,

Hard to say what's happening there... Would you mind pinging me (@lindseydiloreto) on Craft Slack? I'm happy to help troubleshoot this issue.

We'll report the resolution back to this thread for future reference.

lindseydiloreto commented 5 years ago

Got this all sorted out via Slack. Here's the end result for future reference...

It turns out there was a minor redirect happening, where the OP was appending trailing slashes to all URLs. Seems pretty innocent, but it was having a direct impact on the plugin's AJAX calls. When a POST request is redirected, it will be automatically downgraded to a GET request. The plugin's controller method requires a POST request, so it was failing.

The solution was to create an exception to the redirect rule. This allowed the AJAX call to happen normally, and not be subjected to the trailing slash redirect.

RewriteCond %{REQUEST_URI} !^/actions/star-ratings/rate

Putting this line in the .htaccess file prevents the AJAX call from being redirected. 👍