ajax success array bracket property does not return content

Tags: , ,



I am running into an odd behavior that I can’t figure out. I’ve looked through numerous S/O documents without finding a solution. Any help/direction would be appreciated. Thank you.

In essence, I have nested two arrays into the return from an Ajax call and I’ve confirmed the response contains both arrays (json encoding applied to both in the ajax/php file). However, when I try to use the bracket property to extract an element (e.g., response[0] or response[1]), the output is empty.

Here is the Ajax script and the full output:

$.ajax({
            type: "POST",
            url: '/phpscripts/pass.php',  // 
            data: {row : row},
            success:(function(response){
                console.log("response = " + response);
            })
        });

response = [[{"id":"1","learn_load":"2","start_date":"2020-06-29","target_date":"2020-10-01","completed_date":"0000-00-00","bdl_20":"Establish a mentor relationship with Jerry Murray, Director of Finance to learn how to speak their lingo.","bdl_10":"","bdl_70":"Participate in a cross-organization committee to learn how supply chain initiatives reach across business units.","cg":"Business Acumen","cg_orn":"0","cg_tit":"1","cg_lj":"0"},{"id":"2","learn_load":"3","start_date":"2020-06-29","target_date":"2020-08-10","completed_date":"0000-00-00","bdl_20":"Establish mentor relationship with Sharon Yates, Director of Logistics to learn how to enable more automated and optimized business decisions.","bdl_10":"Complete Blue Yonder online certification course.","bdl_70":"Present learning updates to leadership team upon completion of formal training.","cg":"Machine Learning/AI training","cg_orn":"1","cg_tit":"0","cg_lj":"0"},{"id":"3","learn_load":"1","start_date":"2020-06-29","target_date":"2020-09-01","completed_date":"0000-00-00","bdl_20":"Work with a coach to rehearse presentations and other tricky interactions with senior management.","bdl_10":"","bdl_70":"Seek out opportunities to interact informally with top management (e.g., receptions, charity events, off-sites, etc.) to enhance comfort level with them when back at the workplace.rnrn","cg":"Comfort Around Higher Management","cg_orn":"0","cg_tit":"1","cg_lj":"1"},{"id":"4","learn_load":"3","start_date":"2020-06-29","target_date":"2020-08-31","completed_date":"0000-00-00","bdl_20":"Work with a coach to shift mindset from tactical project management to a focus on conceptualizing large-picture projects and foreseeing challenges and needs to be addressed.","bdl_10":"","bdl_70":"Present to analysts throughout the SCM organization tactical vs strategic project planning and how to manage the successful application of both.","cg":"Strategic Project Management","cg_orn":"1","cg_tit":"1","cg_lj":"0"},{"id":"5","learn_load":"2","start_date":"2020-07-01","target_date":"2020-07-31","completed_date":"0000-00-00","bdl_20":"Competency Explicit Mentoring for macro design. Met with Jack Barrow.  ","bdl_10":"Attend Gateway Community College Excel #203 Intermediate Macro Design ","bdl_70":"Create 10 macro functions that could be used by team and provide overview at Wednesday meeting.","cg":"Excel Functions/Macros","cg_orn":"1","cg_tit":"1","cg_lj":"0"}],[{"id":"1000001","cg":"Business Acumen","cg_orn":"0","cg_tit":"1","cg_lj":"0"},{"id":"1000002","cg":"Machine Learning/AI training","cg_orn":"1","cg_tit":"0","cg_lj":"0"},{"id":"1000003","cg":"Comfort Around Higher Management","cg_orn":"0","cg_tit":"1","cg_lj":"1"},{"id":"1000004","cg":"Strategic Project Management","cg_orn":"1","cg_tit":"1","cg_lj":"0"},{"id":"1000005","cg":"Excel Functions/Macros","cg_orn":"1","cg_tit":"1","cg_lj":"0"},{"id":"1000006","cg":"Written Communications (grammar/structure)","cg_orn":"0","cg_tit":"1","cg_lj":"1"},{"id":"1000007","cg":"Leading a Team","cg_orn":"0","cg_tit":"1","cg_lj":"1"}]]

Here is the console.log request using the bracket property:

$.ajax({
            type: "POST",
            url: '/phpscripts/pass.php',  // 
            data: {row : row},
            success:(function(response){
                console.log("response0 = " + response[0]);
                console.log("response1 = " + response[1]);
            })
        });

Console.log Output

I am new at this but I understood that the bracket property would allow me to isolate the two nested arrays using position 0 and position 1. What am I missing here?

Answer

I bet it’s a stringified JSON. To parse it, just do:

response = JSON.parse(response);
console.log("response0 = ", response[0]);
console.log("response1 = ", response[1]);


Source: stackoverflow