I am trying to send array from php to ajax in json file but when i alert res var for testing it i see this error message :
Uncaught SyntaxError: Unexpected token C in JSON at position 0
My array is this :
["C", "Dbm", "Bb", "Bb", "F", "Cm", "Eb", "Dbm", "Bb", "Bb", "F", "Cm", "F", "Bb", "Eb", "Bb", "F",…]
My array created by php function and array item's will different when user clicking on a button in view .
Java Script :
$(".T-chords").on('click',function(event){
event.preventDefault();
var This = $(this);
$.ajax({
url : data.ajax_url,
type : 'post',
dataType: 'json',
data : {
action : 'transpose_callback',
content : data.content,
target_scale : This.text(),
base_scale : data.base_scale,
},
success:function(response){
var res = JSON.parse(response);
alert(res[1]);
},
error: function(){
alert("err");
}
})
})
php code :
function Ajax_transpose_callback(){
header('Content-Type: application/json');
$content = $_POST['content'];
$Target_Scale = $_POST['target_scale'];
$Base_Scale = $_POST['base_scale'];
$Flag_db = "";
$transposed_chord = "";
$transposed_arr = array();
if(preg_grep('/#/', $content)){
$Flag_db = "0";
}
elseif (preg_grep('/b/', $content)){
$Flag_db = "1";
}
else{
$Flag_db = "0";
}
foreach ($content as $item) {
$final_item = substr( $item, 1, - 1 );
$transposed_arr[] = Transpose( $Flag_db, $Base_Scale, $Target_Scale, $final_item );
}
wp_die(json_encode($transposed_arr));
}