-1

I'm sending a request to the server to check a user_id and session_id pair, to make sure that it's valid. This function gets the session data associated with the session_id, and then if the user_id from the session data matches the one passed to the function as an argument, it should execute the callback. Unfortunately, the callback doesn't execute for some reason.

If I type into the console "session_is_valid(2, 1, function() { alert('hi'); });", I can see "success" logged in the console, but no alert. Likewise, if I use an invalid pair, I correctly am notified with a "failure" message. But the callback never executes.

Is this some problem with how I'm using the console in my browser? Or is there a problem with my function?

//Confirm a session id / user id pair

function session_is_valid(session_id, user_id, callback) {
    var data = {'id': session_id};

    return $.ajax({  
      type: "POST",  
      url: ears_path + "?q=session",  
      data: JSON.stringify(data),
      contentType: 'application/json; charset=UTF-8',
      dataType: 'json',
      success: function (result) {
        if(result.user_id === user_id) {
            console.log('success');
            callback
        }
        else { console.log('failure'); }
      }
    });
}
Forrest
  • 151
  • 1
  • 8
  • 4
    And `callback` does nothing. May try `callback()` inatead ? ;) – Jonas Wilms Oct 12 '17 at 18:51
  • @Jonasw actually I use just callback all the time, and it always works perfectly in other functions. How is that possible? – Forrest Oct 12 '17 at 18:51
  • @Jonasw also I don't understand the relevance of the possible duplicate you mentioned. I use AJAX all the time and totally understand these dynamics, I'm not trying to use "return" inappropriately. I'm just trying to make the success callback conditional depending on the result of the query. – Forrest Oct 12 '17 at 18:54
  • 2
    I agree with Jonas. If this snipplet as you have it is reflective of what your actual codes is, then `callback` will do nothing, as it is simply a reference to a method. you have to `callback()` with () to invoke it. – Taplar Oct 12 '17 at 18:56
  • @Taplar Ok! I'll try that! I'm very confused about why so many of my other functions work properly without the () but I'm sure you're right. Thank you for your help. – Forrest Oct 12 '17 at 18:59
  • @forrest yeah the dupe isnt that accurate, but `return $.ajax(..)` is a bit misleading... – Jonas Wilms Oct 12 '17 at 18:59
  • I'm not sure what to do about the question—it seems like the comments solved my issue, but I still think that the duplicate is completely irrelevant, so I don't want to say "yes this solved my issue." – Forrest Oct 12 '17 at 19:00
  • @Jonasw Ah, I see. I've been mostly expanding on code I made that works, but you're right I guess the return is unnecessary on all of my functions. thanks for all your help. – Forrest Oct 12 '17 at 19:01

2 Answers2

-1

There are some comments where you can see the same answer. In my opinion, you will need to make callback() instead of callback to call the function, if you just execute callback you are retrieving and fn object (function object), but you won't execute it. You can check the solution openning the browser console, and writting callback and callback(), if you hace the function in the global scope or if you are debugging the script.

JuanB
  • 180
  • 1
  • 5
-1

This is how I perform mine, modify to suit your needs.

function run_some_ajax() {
    ajax_call(function(result) { //by populating result here, you can reference the data returned from your ajax call
        //success callback logic here
            console.log(result); 

    });

}

function ajax_call(callback) {

    $.ajax({  
        type: "POST",  
        url: ears_path + "?q=session",  
        data: JSON.stringify(data),
        contentType: 'application/json; charset=UTF-8',
        dataType: 'json',
        success: function (result) {
            if(result.user_id === user_id) {
                console.log('success');
                if(typeof callback === "function") { //always check to see if callback is in fact a function
                    callback(result); //place results into your callback function to reference later

                }

            } else { 
                console.log('failure'); 

            }

        }

    }); 

}
Jeff Beagley
  • 945
  • 9
  • 19
  • Hmm why was this downvoted? This shows the proper way of implementing your callback and getting the results into it. – Jeff Beagley Oct 13 '17 at 14:40