Asked  7 Months ago    Answers:  5   Viewed   24 times

As my framework grows i decided to split it into files, instead of leaving it in the main design file. However by doing that the return of a function doesn't return any value.

data isn't empty - if i alert the values in the js file they are there!

The functions:

1st the function in .js file (is included before the execution)

             var lock_get = 0;
             function get_data(data, destination) 
             {

                if (lock_get == 0)
                {
                    lock_get = 1;
                    $.ajax({
                        type: "POST",
                        url: destination,
                        async: true,
                        data: data,
                        success: function(data) 
                        {
                            lock_get = 0;
                            if (data)
                            {
                                return data;
                            }
                        }
                    });
                }
             };

So and here is the execution part:

    var test = get_data(data, destination);
    notice(test);

and test is empty... I already tried different ways for writing but I guess i missunderstood the possibilities of js?

 Answers

91

You can't do that : as the call is asynchronous, the get_data function can't return the result of the ajax call.

What you should do is provide a callback to the get_data function and handle the result in the callback.

function get_data(data, destination, callback) 
         {

            if (lock_get == 0)
            {
                lock_get = 1;
                $.ajax({
                    type: "POST",
                    url: destination,
                    async: true,
                    data: data,
                    success: function(data) 
                    {
                        lock_get = 0;
                        if (data && callback)
                        {
                            callback(data);
                        }
                    }
                });
            }
         };

And call it like that :

get_data(data, destination, function(test){
   notice(test);
});
Tuesday, June 1, 2021
 
o_flyer
answered 7 Months ago
64

If you want to send that value directly to a PHP script, you can use the jQuery post method:

$.post("yourScript.php", { price: yourPriceVar }, function(data) {
    //Success! Do something interesting
});

Alternatively, you could set the value of a hidden input element to your calculated value, so that it can be submitted along with the rest of your form:

$("#hiddenInput").val(yourPriceVar);

HTML:

<input type="hidden" id="hiddenInput" name="price" />
Wednesday, March 31, 2021
 
nighter
answered 9 Months ago
70

First of all new is a reserved word. You need to rename that variable.

To answer your question, Yes, you need to save this in a variable outside the success callback, and reference it inside your success handler code:

var that = this;
$.ajax({
    // ...
    success: function(resp) {
        if(resp == 1) {
            $(that).siblings('.old').html($new);
        }
    }
})

This is called a closure.

Thursday, June 3, 2021
 
LoicTheAztec
answered 7 Months ago
20

When your Ajax-Request succeeds you will have the querystring-variables in the QueryString-Collection of the Request-Object.

Could work like this on the server side:

<% var newdata = Request.QueryString("value1"); %>
Saturday, June 26, 2021
 
Slinky
answered 6 Months ago
64

You need to set async: false for synchronous requests like this:

function doop(){
        var that = this;
        var theold = $(this).siblings('.theold').html();
        var thenew = $(this).siblings('.thenew').val();

        $.ajax({
                async: false,
                url: 'doop.php',
                type: 'POST',
                data: 'before=' + theold + '&after=' + thenew,
                success: function(resp) {
                        if(resp == 1) {
                                $(that).siblings('.theold').html(thenew);
                        }
                }
        });

        // some other code

        return false;
}

see here for details

Friday, July 30, 2021
 
KingCrunch
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share