0

The problem is big, but solve is simple.

I have old function that works and has to be return true or false..

function char_existz(char_name, server) {
    $.ajaxSetup({async: false});
    $.getJSON("/index.php?f=payment&char_name=" + char_name + "&s=" + server, function (json) {
        res = json.k;
    });
    $.ajaxSetup({async: true});
    console.log(res === 1);

    return (res === 1);
}

In using of it i get warning: [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

I want to make it without warning, tried on $.ajax with something like this

function char_existzz(char_name, server) {
    $.ajax({
        type: "POST",
        url: '/payment',
        data: {char_name: char_name, s: server},
        success: function (data) {
            result = (data.k===1);
        },
        dataType: 'json'
    });
    return result;
}
ce = char_existz(character.val(), server.val());

                if (ce === false) {
                    alert('Player not found!');
                    return false;
                }

but it doesn't return true or false in variable, so i don't know how to be, because on old functions it's works, when i don't touch async

Chenmunka
  • 685
  • 4
  • 21
  • 25
Brabus
  • 13
  • 2
  • is this code itself within another function? I note you have another `return false` after the `alert` – ADyson Sep 10 '18 at 15:03
  • Using a callback or doing a bit of reading about Javascript Promises will resolve your problem. It's much better to be making asynchronous calls so the sooner you get your head around this the better. It's not difficult - it's just different than what you're obviously used to. – Reinstate Monica Cellio Sep 10 '18 at 15:11
  • `ce = char_existz(character.val(), server.val()); if (ce === false) { alert('Player not found!'); return false; }` is in another function, but it's ok i will stay like it is working old, and teaching from links that is given at top! Thank you. – Brabus Sep 10 '18 at 16:34

0 Answers0