Skip to content Skip to sidebar Skip to footer

Jquery; How Can I Return Ajax Returned Data?

I'm trying to return AJAX response data like below.. function reqNodelistByField( i ) { $.ajax({ type: 'post', url: './req.nodel

Solution 1:

alert(r) is undefined because you are perfoming an asynchronous call, in other words, your script makes the call and continue executing the code without wait for call's response. For that, you need return the r in the ajax success function.

functionreqNodelistByField( i ) {
            $.ajax({
                type: 'post',
                url: './req.nodelist.by.field.php',
                dataType: 'text',
                data: { field_id:i },
                success: function(data) {
                    r = $.parseJSON(data);

                    if(r.valid == false) {
                         returnfalse
                    }
                    else{
                         returntrue
                     }
                }
            });
        };

Solution 2:

Try Done Docs here.

functionreqNodelistByField( i ) {
            $.ajax({
                type: 'post',
                url: './req.nodelist.by.field.php',
                dataType: 'text',
                data: { field_id:i },
            }).done(function(r){
                alert (r)
                console.log(r)
                });
        };

Solution 3:

try this :-

functionreqNodelistByField( i,callback) {
        $.ajax({
            type: 'post',
            url: './req.nodelist.by.field.php',
            dataType: 'text',
            data: { field_id:i },
            success: function(data) {
                r = $.parseJSON(data);
                callback(r);
                //if(r.valid === false) r = false;
            }
        });

    };

use it like :-

reqNodelistByField("value of i",function(r){
// here you use the r i.e data from ajax request// check  here if(r.valid === false){
   }
});

Post a Comment for "Jquery; How Can I Return Ajax Returned Data?"