0

So, I'm able to return my AJAX request successfully, but my jQuery seems to die once I declare a variable from it.

Here's my JSON response from the console:

Object {readyState: 4, responseText: "{"rsp":"1","msg":"show out screen!","time":null,"fn":"Mike","ln":"Maynard","ul":"0"}", status: 200, statusText: "success"}

Here's my jQuery:

    $.ajax({
        url: "clock.php",   
        type: "POST",   
        async: false,
        data: clockData,
        cache: false,
        timeout: 5500,
        error: function (clockData) {
        //var rsp = (clockData.fn);
        alert('Error');
            //do error
        },


        dataType: 'json',
        complete: function (clockData) {            
            console.log(clockData);
            var rsp = clockData[0].rsp;

            console.log(rsp);

            var ul = clockData[0].ul;   
            if(ul=='1') { 
                showUi(); 
            }

            var una = (clockData.fn + ' ' + clockData.ln);
            $('.nameBlock').text(una);
            $('.nameBlockFirst').text(clockData.fn);
            //--> show in ui

            if (rsp=='0') {
                console.log('got here2');
                var dir = 'In'; tcShow(dir);
            }   

            //--> show out ui                           
            if (rsp=='1'){
                alert('trying to show out screen2');        
                var dir = 'Out'; tcShow(dir);
            }

            //--> show in result
            else if (rsp=='2'){
                var time = (clockData.time); var dir = 'in'; showResult(time,dir,ul);
            }
            //--> show out result
            else if (rsp=='3'){
                var time = (clockData.time); var dir = 'out'; showResult(time,dir,ul);
            }
            //--> show message
            else if (rsp=='4'){
                endClock();
            }                   
            else {
                endClock();
            }
        }
    });

So, console.log(clockData); Returns fine, but console.log(rsp); Never happens... I'm confused..

1
  • Here's the output of clockData: Object {rsp: "1", msg: "show out screen!", time: null, fn: "Mike", ln: "Maynard"…} Commented Jul 12, 2016 at 18:04

1 Answer 1

1

Based off your response text, it looks like it should be clockData.rsp. You are doing clockData[0].rsp which would imply that clockData is an array. But in fact, your response is a keyed object, not an array.

EDIT: I just noticed you are also using the complete method, not success. complete has a method signature of (jqXHR, textStatus). If you want the response data, you can access it through JSON.parse(clockData.responseText), or better yet, use the success callback which has a method signature of (responseData, textStatus, jqXHR). Or for a more modern approach, use promises.

REF: http://api.jquery.com/jquery.ajax/

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

3 Comments

var rsp = clockData.rsp; returns undefined :(
What is the output of console.log(clockData) ?
Also just realized you are using the complete method for your callback, which has a signature of (jqXHR, textStatus). clockData in this case will be the request object itself, not your result. You should be using the success method, or better yet, AJAX promises. api.jquery.com/jquery.ajax

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.