0

I've followed this old guide which didnt work, probably due to it being for Craft 2, so I modified it to what I assumed would work.

I changed the /actions/tags/createTag to /actions/tags/create-tag as apparently this is what Craft 3 needs.

I also updated the code to include the CSRF token when posting data via jQuery.post() as well using this Craft guide

But I was still getting errors with the following code

      <div id="tags">
        {% for tag in craft.tags.group('skills').limit(null) %}
            <label>
                <input type="checkbox" name="fields[skills][]" value="{{ tag.id }}">
              {{ tag.title }}
            </label>
        {% endfor %}
    </div>

<a id="new-tag">+ New tag</a>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<script type="text/javascript">
    window.csrfTokenName = "{{ craft.app.config.general.csrfTokenName|e('js') }}";
    window.csrfTokenValue = "{{ craft.app.request.csrfToken|e('js') }}";
</script>

<script type="text/javascript">

    $('#new-tag').click(function()
    {
        var name = prompt('What’s the new tag name?');

        if (!name) return;

        var data = {
            'groupId': '2',
            'title' : name,
        };

        var myJSON = JSON.stringify(data);

        $.post('/actions/tags/create-tag', myJSON, function(response, textStatus, jqXHR)
        {
            if (response.success)
            {
                $('<label>' +
                      '<input type="checkbox" name="fields[skills][]" value="'+response.id+'" checked="checked"/> ' +
                    name +
                  '</label>').appendTo($('#tags'));
            }
            else
            {
                alert('Unable to create that tag.');
            }
        });

    });

</script>

I got a 400 bad request and it says 'unable to verify your data submission'

So I turn off CSRF protection in the general just to see if anything else is wrong.

Then I get a different bad request saying 'Bad Request: Request must accept JSON in response'...

So I have no idea what or why this isn't working. I also altered the orignal code to JSON.stringify the data as originally I was getting an error that I had to send in JSON.

Anyway I can't work out what else is wrong so would love some help.

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.