Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
JQuery: storing AJAX response into a global variable

Please would someone help me to update a global variable (user_ID) with the response from an AJAX request.

I have attempted the following with no success - https://newbedev.com/jquery-storing-ajax-response-into-global-variable. Alert() has consistently stated global var was undefined,

Note the console.log() message states a valid user_ID in the inner-scope. I just need to get it out..

Thanks :-)

// update global variable (user_ID) with ajax response

var user_ID;  // empty var to be populated with result
function receiveUserId(layerURL) {
    $.ajax({
        url: layerURL,
        crossDomain: true,
        type:"GET",
        // process the response from the server
        success: function(result){
            user_ID = result.user_ID; //JSON obj.prop
            console.log("The server responded with the following user ID: ", user_ID)
            return user_ID
            }
        });
}

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!