2

I'm working on a Cloudflare integration plugin for Craft 3. I set up the plugin with settings, which works fine so far. Now I intended to verify the Cloudflare credentials using an AJAX call.

I set up a controller at controllers/DefaultController with a method named actionFetchZones:

class DefaultController extends Controller {
    protected $allowAnonymous = [ 'fetch-zones' ];

    public function init() {
        parent::init();
        $this->defaultAction = 'fetch-zones';
    }

    public function actionFetchZones() {
        return $this->asJson( Plugin::getInstance()->cloudflare->getZones() );
    }
}

I registered the action in the plugin class:

// register the actions
Event::on(
    UrlManager::class,
    UrlManager::EVENT_REGISTER_CP_URL_RULES,
    function( RegisterUrlRulesEvent $event ) {
        $event->rules['POST cloudflare/zones'] = 'cloudflare/fetch-zones';
    }
);

But the response is

POST https://host.tld/admin/actions/cloudflare/zones 404 (Not Found)

When I request /admin/actions/cloudflare instead, I receive a 200 response.

The plugin handle is set to cloudflare, the app works fine otherwise. What am I doing wrong?

Edit: As requested, below is the AJAX call:

Craft.postActionRequest(
  'cloudflare/zones',
  { apiKey, email },
  (response, textStatus, request) => { /* ... */ }
);
  • 1
    The most important part is missing... The ajax request. Did you define your routes snake-case and include a csrf token in your request? I'm not sure if you can leave the controller name blank if you name it default just a guess but what happens when you do cloudflare/default/fetch-zones – Robin Schambach Dec 7 '17 at 18:12
  • @RobinSchambach I added the AJAX call, nothing too special though. I supposed the CSRF would get included by Craft.postActionRequest? – Moritz Friedrich Dec 8 '17 at 6:08
  • Not sure. You can see it in your browser console network tab. Or I can try it for you in about 1.5h. Since I always use angular I have to include it on my own. Or you can deactivate the token in your config. If it works afterwards you know what you have to do – Robin Schambach Dec 8 '17 at 6:15
  • Ok I tested it. Craft.postActionRequest sends the CSRF token. When I copy your code it does not work either for me, but when I do Craft.postActionRequest('cloudflare/default/fetch-zones'..... everything is fine – Robin Schambach Dec 8 '17 at 7:23
  • I'm on my way into the office, will test and get back to you ASAP :) – Moritz Friedrich Dec 8 '17 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.