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

297
Views
jQuery ajax return value

How can I return the value "pinNumber" from jquery ajax so I can append it outside of the ajax. Here is my code

var x = pinLast + 1;
    for(i=x;i<=pinMany;i++) {
        var i = x++;
        var cardNumber = i.toPrecision(8).split('.').reverse().join('');
        var pinNumber = '';

        jQuery.ajax({
            type: "POST",
            url: "data.php",
            data: "request_type=generator",
            async: false,
            success: function(msg){
                var pinNumber = msg;
                return pinNumber;
                //pin number should return
            }
        });

        jQuery('.pin_generated_table').append(cardNumber+' = '+pinNumber+'
'); // the variable pinNumber should be able to go here }

Please ask me if you don't understand.. ^^ thanks

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

AJAX is asynchronous by default, you cannot return a value from the callback without making a synchronous call, which you almost certainly don't want to do.

You should supply a real callback function to the success: handler, and put your program logic there.

over 4 years ago · Santiago Trujillo Report

0

var pinNumber = $.ajax({
    type: "POST",
    url: "data.php",
    data: "request_type=generator",
    async: false
}).responseText;
jQuery('.pin_generated_table').append(cardNumber+' = '+pinNumber+' ');
over 4 years ago · Santiago Trujillo Report

0

It has to do with variable scope. The local variable pinNumber you create is not accessible outside its wrapping function.

Perhaps declare pinNumber globally or if it'll do the trick, simply stick your .append() inside your success function.

over 4 years ago · Santiago Trujillo Report
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!