Hot answers tagged

3

Here's a cut-down version of what I'm using on https://cld.agency/showcase It's using jQuery because that's the way I like it: {% set theLayout = craft.app.request.isAjax and not craft.app.request.isLivePreview ? '_ajaxLayout' : '_layout' %} {% extends theLayout %} {% set entries = craft.entries.section('news').limit(12) %} {% paginate entries as pageInfo, ...


3

Your code looks fine and worked for me when testing locally. However it could be failing due to the fact that you're not url-encoding the parameter before you send the request. If the values contain spaces or other special characters, for example, this would make it fail. To fix that issue, try using encodeURI() like this: $("#second-choice").load("{{ ...


2

The Sprig plugin makes this rather straightforward to achieve. Any time the category is changed, the component will re-render itself and display the resulting entries. {{ sprig('_components/filteredEntries') }} {{ sprig.script }} {#--- _components/filteredEntries ---#} {% set categoryId = categoryId ?? '' %} <select sprig name="categoryId"&...


2

The controller action you are hitting returns a template and I think that it is why you are getting this error. If you don't have headlessMode on (docs) you could make a quick template, add: {% header "Content-Type: application/json"%} {{ craft.commerce.countries.getAllCountries() | json_encode() | raw }} Which would give you an endpoint you can ...


2

You need to provide a CSRF token, alternatively turn it off in your config/general.php file. Set a token in your twig template: {% set csrfToken = { csrfTokenName: craft.app.config.general.csrfTokenName, csrfTokenValue: craft.app.request.csrfToken, } %} <script type="text/javascript"> window.Craft = {{ csrfToken|json_encode|raw }}...


2

So normally in your main layout twig template, you'd have something like this to set some JS variables to hold the current CSRF token: {# Super preliminary JS Stuff #} <script type="text/javascript"> window.csrfTokenName = "{{ craft.app.config.general.csrfTokenName|e('js') }}"; window.csrfTokenValue = "{{...


2

I had a same problem and I will try to give you more detailed explanation. Craft thinks you submitted a classic login form and Users controller tries to redirect you after successful login. In case you send AJAX request Craft will return success:true. So, you should add dataType: "json" in your AJAX request and you will be logged in with response ...


1

After enabling the headless mode in Craft I'm getting the same error in JSON format: Anyway I guess I have to write some module for that extra functionality. Thank you for the tips!


1

Try just POSTing the new absolute quantity you want to change to - e.g. if you have 5 in your cart and want to decrement by one, you'd POST something along these lines: { "redirect": "6bb2a73aac3479c68e0946e4455624e2c1b840bc2b6356074a51ba01ee1234d/cart", "action": "commerce/cart/update-cart", "...


1

I'd suggest generating the code via pluginfactory.io and if you have code comments enabled, it'll give you the URI that you need to trigger your action in the example code. But essentially it's going to be a kebab-case version of your controller route, with a kebab-case version of your method name (but removing the action preface). For you, first you should ...


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-...


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 ...


1

Finally found the answer after much time spent on it. The $(this).serialize() loses the file input - and can't submit. Updated ajax submit to $.ajax({ type: 'POST', url: url, data: new FormData(this) , dataType : 'json', // changing data type to json processData: false, contentType: false, success: function (response) { // ...


1

The path to the ajax file was wrong. I had it as: $.ajax("templates/ajax?search=" + value, { when it should have been $.ajax("ajax?search=" + value, {


1

You'll need to set the X-Requested-With header to XMLHttpRequest. const response = await fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest', }, });


Only top voted, non community-wiki answers of a minimum length are eligible