AJAX Framework using dynamic data request

hey guys,

With October CMS v1.x AJAX Framework, this code was working:

'data-request': 'onChangeCountry',
'data-request-data' : "'selectedCountryId': $('#country_id').val()",

it looks like it is not working anymore with OC v2.x.

JS console log error:

Uncaught Error: Error parsing the data-request-data attribute value. Error: Broken JSON body near Id': $('#country_id').val()}
    at paramToObj (framework.js:512:19)
    at $.fn.request (framework.js:480:19)
    at HTMLSelectElement.documentOnChange (framework.js:532:17)
    at HTMLDocument.dispatch (8f55f5b1b94742b550206e36b52f7945-1654042264:2:43064)
    at v.handle (8f55f5b1b94742b550206e36b52f7945-1654042264:2:41048)

Hi @chris,

That approach has not worked for a while, since it is using eval() which was removed some time ago. You may want to place the country_id input inside your form or use the ajaxSetup event to inject the data programmatically.

$(document).on('ajaxSetup', function(event, context) {
    context.options.data.selectedCountryId = $('#country_id').val();

Taken from: JavaScript API - October CMS - 2.x

1 Like

hi @daftspunky,

yes it makes sense now as I am taking over an old project. It probably worked at a time but nobody notice that the expected behaviour was not here anymore.

so the current code is attached to a select input. The country id field is the select itself. I just want to show an input field 1 or a field 2 based on the selected element when it changes.
As I am writing this, I am guessing that I actually do not need to send data in the request, is that correct?

If the country id field is the select itself, that is correct; it will be included automatically.

1 Like