-2

Is it possible to access request parameters in function callback of jquery get

 function doSomething() {
     var level;
     $.get('http://example.com/getLevel', {
         'login': user
     },

     function (resp) {
         //any way to access user or assign resp to level here?
         level = resp; //does not work here.
     });

     //at this point level is still undefined.
 }

Thanks

Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42
RandomQuestion
  • 6,778
  • 17
  • 61
  • 97
  • 2
    possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Satpal Feb 21 '14 at 09:53

2 Answers2

1

Ajax is asynchronous which is the reason why level is undefined at the commented point in your code. If you need to assign the response to level and use that variable you must include that code within the success handler.

Currently the code that attempts to use level (code written after the ajax call) is executed prior to the ajax call returning a response. This causes level to be undefined.

     function doSomething() {
           var level;
           var data = {'login':user}; //assigning data outside of get invocation
           $.get('http://example.com/getLevel', data,function(resp){
               //any way to access user or assign resp to level here?
               level = resp; //does not work here.
                //at this point level is defined and should be used in the code.
            });
            //data could now be used here
      }
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • Sry, I don't see difference between your code and mine. It just define separate variable as `data`. How this helps in assignment of `level`? – RandomQuestion Feb 21 '14 at 09:58
  • The comments in your code indicate you want to do two things, access user or assign resp to level. Moving data outside of the method invocation will allow you to use `{'login':user}` within other parts of the code. Keeping all of the `level` related code inside the successhandler will guarantee that level is assigned the response. – Kevin Bowersox Feb 21 '14 at 10:02
1

How to return the response from an AJAX call will give you idea of what I am talking here.Please check comments inline

function doSomething() {
    var level;
    var data = {
        'login': user
    };

    $.get('http://example.com/getLevel', data, function (resp) { // js dont wait (Thats why called  Asynchronous JavaScript and XML) for this request to complete and  immediately executed next line of code.This is called callback function where your code gets executed after you receice responce from AJAX call.
        level = resp;
        // you can use level here 
        callback(resp);            // // Or use callback function and pass it response once you receive it.
    });
    // Here it could be possible that your your response is no yet received so your level variable still undefined.             
}

function callback(level) {
    // use level here
}
Community
  • 1
  • 1
Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79