1

I have a piece of code that retrieves the last question on a program and it is suposed to update the values of HTML5 progress bars with the latest values.

Now for some reason that I cannot find, when I console.log the data parameter, it is full. But when I try to use it nothing is shown.

Is there something wrong in the code that I cannot realize?

enter image description here

//Run timer
$(document).ready(function () {
    if($('input[name=active_question]').val()!="")
    {
        start_timer();
    }
});
function start_timer()
{
   var x=0;
    function doStuff() {
       x++;
       $('.timer').html(x+' sec');
       console.log('Timer:'+x);
       $.ajax({
        method: "GET",
        url: "/services/get_active_question/"
        })
        .done(function( data ) {
            //It is time to regenerate the question values.
            console.log('Data:'+data);
            if(data['id']!=0)
            {
                regenerate_question(data);
            }
        });
    }
    setInterval(doStuff, 1000); 
}
function regenerate_question(data)
{
    if(data['id']!=0)
    {
        console.log('A: '+data['a']);
        $('.progress-a').prop('value',data['a']);
        $('.progress-b').prop('value',data['b']);
        $('.progress-x').prop('value',data['x']);
        $('.progress-y').prop('value',data['y']);
    }
}
6
  • dataType: 'json' add this to your ajax call Commented Feb 11, 2016 at 15:20
  • or add data = JSON.parse(data) in done. Commented Feb 11, 2016 at 15:23
  • Thanks man. That totally solved. How come that in other scripts were I don't use it but I pass POST parameters I don't get this problem? Commented Feb 11, 2016 at 15:40
  • which one did really fix it?\ Commented Feb 11, 2016 at 15:43
  • 1
    I'll post the details as answer. Commented Feb 11, 2016 at 15:45

1 Answer 1

1

Your return from the ajax is json string. But the ajax is not identifying it as JSON. So you'll need to specify the dataType as JSON.

$.ajax({
    method: "GET",
    url: "/services/get_active_question/",
    dataType: 'json'
}).done(function( data ) {
    //It is time to regenerate the question values.
    console.log('Data:'+data);
    if(data['id']!=0)
        regenerate_question(data);
});

Alternative way is to use

data = JSON.parse(data) 

in the done function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.