Return AJAX response to another function

0

I have two functions

First function:

sendRequest: function () { 
 jQuery.ajax({
        type: 'POST',
        url: xtremeSearchUrl,
        dataType: 'json',
        async: true,
        data: JSON.stringify(data),
        success: function (data) {
        data = this.searchBP();
        if (data == false) {

        }else{

        }
    },error{

    }});
    }

Second function:

searchBP: function () {
    var $url = "/bP";
    var $data = {url: window.location.pathname};
    jQuery.ajax({
        type: 'POST',
        url: $url,
        data: $data,
        success: function (result) {
            console.log(result);
            return true;
        },
        error: function (xhr, status, error) {
            console.log(xhr);
            console.log(status);
            console.log(error);
            return false;
        }
    });
}

I understand that ajax runs asynchronously and when you put the async in false pause the whole page, what I need to do is that when you call the function searchBP () nothing else will be executed until you return the true or false and handle according to that answer another block of code in sendRequest

    
asked by Lina Cortés 31.01.2018 в 17:59
source

2 answers

2

Your function searchBP returns nothing. When you do data = this.searchBP(); data it does not have the value with which searchBP is resolved but it is simply undefined .

The correct way would be for searchBP to return a promise (jQuery.ajax returns promises)

searchBP: function () {
    var $url = "/bP";
    var $data = {url: window.location.pathname};
    return jQuery.ajax({
        type: 'POST',
        url: $url,
        data: $data
     }).then(function (result) {
        console.log(result);
       return true;
     }).catch(function (err) {
        console.log(err.status);
        console.log(err.statusText);
        return false;
     });
}

and then you could use it in sendRequest as

sendRequest: function () { 
  var _this = this; // para preservar la instancia de tu objeto
  jQuery.ajax({
        type: 'POST',
        url: xtremeSearchUrl,
        dataType: 'json',
        async: true,
        data: JSON.stringify(data),
        success: function (data) {
           _this.searchBP()
             .then(function(response) {
                if (response == false) {
                  console.log('searchBP es false');
                } else {
                  console.log('searchBP es true');    
                }
           });

    },error{

    }});
}

You should still get used to using then and catch when executing jQuery.ajax instead of using success and error that function as callbacks and do not adhere to the standard of use of promises.

eye that catch is used from jQuery 3. In jQuery 2.x fail is used.

    
answered by 31.01.2018 / 18:27
source
0

Already you answered yourself. Do your first function that is not asyncrona with async: false , do not perform the second until you reach success.

sendRequest: function () { 
 jQuery.ajax({
        type: 'POST',
        url: xtremeSearchUrl,
        dataType: 'json',
        async: true,
        data: JSON.stringify(data),
        success: function (data) {
        data = this.searchBP();
        if (data == false) {

        }else{

        }
    },error{

    }});
    }
    
answered by 31.01.2018 в 18:02