Well folks, I have a problem here with DateTimePicker . I'm trying to set the dynamically accepted times with the allowTimes attribute. To get easier the code tá asism:
$('#datetimepicker2').datetimepicker({
datepicker: false,
format: 'H:i',
onShow: function () {
var id = $('#id').find('option:selected').val();
var horarios = PegaHorarios(id);
console.log("allowTimes:"+horarios);
this.setOptions({
allowTimes:horarios
});
}
});
Quickly explaining the code: I take the 'id' to do a search with the function PicksTime (), using AJAX , I put the 'dataType' as a 'script' (already tried json, html none worked), the function returns me the values correctly, which I tested via console.log:
allowTimes: ['14:00', '14:20', '14:40', '15:00', '15:20', '15:40', '16:00', '16 : 20 ',' 16:40 ',' 17:00 ',' 17:20 ',' 17:40 ']
So I do not know what to do ... I tried some options and nothing, does anyone have any idea how to fix it?
Thank you.
A, just to note I copied and pasted this log into the function and the code worked ...
Edit 1: This is the PegaTime function.
function PegaHorarios(id) {
var retorno = "";
carregando(true);
$.ajax({
type: 'POST',
dataType: 'script',
url: 'includes/monta-horas.php',
async: false, // não achei método alternativo
// Valor default é true então essa opção é desnecessária agora - async: true,
data: 'id='+id,
success: function (response) {
//console.log("Sucesso no envio da solicitação Ajax");
//console.log(response);
carregando(true);
retorno = response;
},
error: function (response) {
//console.log("Erro na Solicitação Ajax");
//console.log(response);
retorno = false;
},
complete: function (response) {
//console.log("Solicitação Ajax Completada");
//console.log(response);
carregando(false);
}
});
return retorno;
}
In case the return comes from a PHP code that returns the String that is shown in the console.log.
//echo json_encode($retorno);
echo $retorno;
I used json_encode when the dataType was json and the result was the same.
I do not remember if I used JSON.parse @Marconi , but I used it and it did not work.
Edit 2: To complete a summary, by showing the time in the datetimepicker I call the function
PegaHorarios()
with the value of a select field through
$('#id').find('option:selected').val();
In the PegaHararios () function I make an AJAX request to a PHP page that returns a String with var_dump:
string(97) "['14:00','14:20','14:40','15:00','15:20','15:40','16:00','16:20','16:40','17:00','17:20','17:40']"
I get this string and return the function to the
var horarios;