I have this JSON response code from my script:
{
"id": 159690,
"name": "Product name",
"categories": {
"2068": "Category one",
"1760": "Category two",
// ...
}
}
I want to append checkboxes to #chkrole
the with value and name from the JSON response. Instead, my code is giving me the following error.
Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
Why is this happening? Here is my code:
$.ajax({
url: '<?php echo base_url(); ?>admin/grab_product_next',
data: form.serialize(),
type: 'post',
dataType: 'JSON',
success: function(resp) {
$('input#name').val(name);
var json = JSON.parse(resp.categories);
var val = 0;
var table = $('<table></table>');
var option = json.map(x =>
table.append($('<tr></tr>').append($('<td></td>').append($('<input>').attr({
type: 'checkbox',
name: 'chkRoles',
value: x.chkName,
id: 'chkrole' + val
}))).append(
$('<label>').attr({
for: 'chkRoles' + val++
}).text(x.chkName))));
$('#chkrole').append(table);
}
})