Javascript issue

I am trying to make this function work. I think this function is pretty clear:

function FileExists(path){
    var result = false;

    $.ajax({
        url:    "http://mydomain.com/"+path,
        type:   "HEAD",
        success:
                function(){
                    result = true;
                }
    });

    return result;
}

I need an anonymous function that is called by the success of the ajax post to set the result variable that was defined inside the FileExists function to true so that I can return this value from the FileExists function. I think I need to close this, but they confuse me.

Please, help! Thank!

+3
source share
2 answers

Ajax calls are asynchronous by default, you can use the callback function:

$.ajax({
    url:    "http://mydomain.com/"+path,
    type:   "HEAD",
    success: function(){
        callback(true);
    }
});

Or make a call synchronously.

$.ajax({
    async:   false,
    url:    "http://mydomain.com/"+path,
...
+2
source

, , $.ajax , , FileExists return $.ajax. , , result, success.

+3

Source: https://habr.com/ru/post/1796596/


All Articles