nick nick - 1 year ago 70
jQuery Question

How can I get responseJSON from ajax reponse object

Edit: Solved by doing what I needed in ajax().done, didn't know that was one of the callbacks available til I looked into this more.

Ok, here is my code. I am trying to get some ids that are filled in with php then use ajax to run a query to see if some related property is set or not. I want to access the responseJSON property, but if I try to access directly it is undefined. However, if I just write the whole variable to console it's all there so I guess I'm missing something. I also tried copying the response to a global variable, but that just gives me the empty value I set in the beginning. How can I access the response?


var valid;

//get all appointment numbers

appointment_nums = [];
$('.mgrid_table > tbody > tr').each(function() {

var temp = ($(this).find('td').eq(3).find('label').html());
appointment_nums = appointment_nums.filter(function(n){ return n != undefined });
appointments = appointment_nums.length;

scheduled = $.ajax({
url: "../testrequest.php",
data : {appointment_nums:appointment_nums},

$('table:nth-of-type(2) > tbody > tr > td:nth-of-type(2)').each(function() {

if($(this).children().length < 1){
$(this).append('<a href="index.php?doctor=test&a='+appointment_nums[count]+'">Schedule Appointment </a>');
count = count + 1 ;



$appointment_numbers = (isset($_POST['appointment_nums'])) ? $_POST['appointment_nums']:0;
if ($appointment_numbers!=0){
$appointments_scheduled =array();
foreach ($appointment_numbers as $apt_num){
$sql = 'SELECT event_id FROM '.TABLE_APPOINTMENTS_SCHEDULING.' WHERE appointment_number = "'.$apt_num.'"';
$res = database_query($sql);
array_push($appointments_scheduled, empty($res));


Edit: I don't think this is a duplicate, it's a slightly different way of getting that same info but my issue is not the same. If I do console.log(scheduled) anywhere, I get the response object with everything set. So if I see the data, it must be there and it's showing after the response is made. But then I can't access the object, I'll put a picture to show what I mean. Also, valid and scheduled are two attempts to get the same info. The other posts don't help with the way I am trying to do it, I've checked.

enter image description here

Answer Source

In your $.ajax method, property done is a callback function that executes after successful response. Only there and than you can access your json:

scheduled = $.ajax({
    url: "../testrequest.php",
    data: {appointment_nums:appointment_nums},
    dataType: 'json',
    done: function(response){

More about jQuery ajax method here.

In case you want to use async ajax call, than you need to set:

async: false

async (default: true) Type: Boolean By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.