-1

I would like to make multiple Ajax calls from my index.html.

One call comes from $(document).ready() function. Second one is invoked from .on("click") event outside the $(document).ready() loop.

Unfortunately, the second call seemingly takes success callback from the first call. I tested this by calling a.php in first call and b.php in second call. Both calls end up in a.php, and both have identical parameters. Is there a way to make this work in Craft?

  • From your description, this sounds more like a JavaScript issue than a Craft CMS issue. Could you post a code snippet to better describe what you're trying to achieve? – Martin Spain Aug 11 '20 at 15:50
  • 2
    I’m voting to close this question because it's not related to Craft. – Ian Aug 11 '20 at 22:15
1

The problem seems related to my structure. Yii expects certain controller name, and since I only used a default naming convention in a single controller, Yii keeps calling this controller only.

The problem is that I have no idea how to structure my plugin correctly. https://craftcms.com/docs/2.x/plugins/controllers.html#posting-to-controller-actions-with-javascript doesn't work for me and 3.x documentation doesn't mention it at all.

  • I would recommend using pluginfactory.io to create your initial plugin framework, which will give you a good 'best practice' outline of how a Craft plugin should be structured. It's also worth looking at the source code of a few Craft plugins to see how their authors have structured their code. – Martin Spain Aug 12 '20 at 13:15
1

Craft 3.x controller endpoints are kebab-case rather than camelCase as with Craft 2.x, and you can post to them via JS very easily using the URL format below:

https://yoursite.com/index.php?p=actions/<plugin-handle>/<controller-name>/<controller-action>

To take the example from the old Craft 2 docs you linked to above, you can make an Ajax request to the default endpoint of a controller like this:

https://yoursite.com/index.php?p=actions/cocktail-recipes/ingredients

Or a specific (i.e. non-default) controller action like this:

https://yoursite.com/index.php?p=actions/cocktail-recipes/ingredients/save-ingredient

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.