-1

I am working on my jquery code to call a function to get the value I want when I refresh on a page so I can get the return value.

When I try this:

var email_number = '';

// check if page refreshed or reloaded
if (performance.navigation.type == 1) {
    var hash    = window.location.hash;
    var mailfolder = hash.split('/')[0].replace('#', '');
    var emailid = 'SUJmaWg4RTFRQkViS1RlUzV3K1NPdz09';
    decryption(emailid, mailfolder);
}

function decryption(emailid, mailfolder) {

    $.ajax({
        url: 'decrypt.php',
        type: 'POST',

        data : {
            emailid: emailid,
            mailfolder: mailfolder
        },

        success: function(data)
        {
            email_number = data;
        }
    });
    return email_number;
}

I will get the empty value. So I have tried this:

function decryption(emailid, mailfolder) {
    var email_number = '';

    $.ajax({
        url: 'decrypt.php',
        type: 'POST',

        data : {
            emailid: emailid,
            mailfolder: mailfolder
        },

        success: function(data)
        {
            email_number = data;
        }
    });
    return email_number;
}

And I have also tried this:

return;

I still get the empty value as a return. I have checked on a variable called email_number and the return value is 6. How do you get the return value when I refresh on a page to call a function that will run via ajax to store the value in the variable email_number?

Thank you.

Robert Jones
  • 390
  • 3
  • 18
  • @ASDFGerte I have already tried it but no luck. Any idea? – Robert Jones Oct 01 '19 at 20:17
  • You're calling the function like this: `decryption(emailid, mailfolder);` so you're discarding the potential return value anyway. The short answer is: whatever you want to do with `email_number`, do it *inside* the `success:` function. –  Oct 01 '19 at 20:24
  • @ChrisG I want to store the value `6` in the `email_number` variable. I have put the `email_number` inside the `success:` function and I have checking the `email_number` as I get the return value so how I can store the `email_number` value outside of the `decryption` function? – Robert Jones Oct 01 '19 at 20:31
  • @ChrisG I tried to use `email_number = decryption(emailid, mailfolder);` but I get undefined. Any idea? – Robert Jones Oct 01 '19 at 20:32
  • Please go read the link we've supplied. Even the `return` must be in `decryption`. You simply can't work with `email_number` outside of the `success` function. – Scott Marcus Oct 01 '19 at 20:33
  • If you want to store the result for later use, you need to move `var email_number;` outside of all functions. That way you can call `email_number = data;` in the success function, then refer to `email_number` at a later point in time. –  Oct 01 '19 at 20:35
  • @ChrisG I have already done that as I have moved the `var email_number` outside of the function, but i still get no result when I try to use `alert(email_number)` after the `decryption(emailid, mailfolder);`. Any idea? – Robert Jones Oct 01 '19 at 20:37
  • Again; if you refer to `email_number` directly after calling `$.ajax()`, the ajax call is still underway. JS doesn't wait for it to finish, that's why you have to state a `success` *callback*. If you want to write synchronous code, you have to wrap the `$.ajax` call in a `Promise` and use `async` / `await`. Try this: https://pastebin.com/Ma0yQE77 –  Oct 01 '19 at 20:38
  • thank you but it doesn't work – Robert Jones Oct 01 '19 at 20:46
  • ??????????????????????????? – Robert Jones Oct 01 '19 at 21:00

1 Answers1

-2

As ajax call is asynchronous, by the time control executes return email_number; ajax is busy fetching data.

Refer this: How do I return the response from an asynchronous call?

Rahul Pal
  • 96
  • 8
  • 3
    This should be a comment, not an answer. I see you don't have enough rep to post a comment, instead, you should answer questions which don't require clarification. You could also potentially get enough rep to post comments by submitting edits to questions/answers and having them be approved by the community. – GrumpyCrouton Oct 01 '19 at 20:27
  • @GrumpyCrouton thank you for your suggestion, I got it. – Rahul Pal Oct 01 '19 at 20:44