0

I have an ajax function in javascript that hold data, and when i try to return it get nothing.
my code:

function get_conv_data(click_id,server) {  
var tmp; 
new Ajax.Request(ajaxUrl, 
    {   
        method: 'post',
        parameters: {
            'act': 'get_conv_data',                
            'click_id': click_id,
            'server':server              
        },
        onSuccess: function(res) 
        {
            alert(click_id+'xx'+server);

         tmp=res.responseText; 
        }
    }); 
    return tmp;
}

the data is 'alive' inside the ajax( the alert print is good) but now i want to return it into php array i get no result , why is that? thanks

user2706762
  • 397
  • 1
  • 3
  • 11

6 Answers6

2

It is because return tmp runs before the onSuccess

function get_conv_data(click_id,server, cb) {  
var tmp; 
new Ajax.Request(ajaxUrl, 
    {   
        method: 'post',
        parameters: {
            'act': 'get_conv_data',                
            'click_id': click_id,
            'server':server              
        },
        onSuccess: function(res) 
        {
            cb(res.responseText);
        }
    }); 
}

get_conv_data((YOUR ID THING HERE),function(data){
    console.log(data);
});

Do it like that.

smistry
  • 1,127
  • 1
  • 9
  • 9
1

Ajax is by definition Asynchronous. That means, it executes in the "background" while execution of the rest of the program continues. So get_conv_data will return while the Ajax executes. On successfully getting data from the server it will execute the onSuccess method (that is when your "alive" gets printed").

So execution timeline goes like :

get_conv_data --> initiate Ajax --> return;
                           Ajax request--> Server response --> onSuccess call;

Method suggested by @smistry is a good way to go.

Varun Achar
  • 14,781
  • 7
  • 57
  • 74
1

Javascript framework is single thread process and ajax run in separate thread, make ajax as sync using async: false, this will make ajax normal method/function

A.T.
  • 24,694
  • 8
  • 47
  • 65
1

Ajax calls are asynchronous. This means that return tmp is called immediately after Ajax.Request is called without waiting for the response of the ajax request. In order for your variable to contain the value of the response you either can set an async: false inside the Ajax.Request body

var tmp
new Ajax.Request(ajaxUrl, 
{   
    method: 'post',
    async: false,
    parameters: {
        'act': 'get_conv_data',                
        'click_id': click_id,
        'server':server              
    },
    onSuccess: function(res) 
    {
        alert(click_id+'xx'+server);

     tmp=res.responseText; 
    }
}); 

or perform the actions you want inside the onSuccess function.

Hope that helps

Varun Achar
  • 14,781
  • 7
  • 57
  • 74
Nickey
  • 1,191
  • 1
  • 13
  • 21
1

Ajax is meant to be async

Ajax is meant to be async so you can't return the response inside the function itself. You need to create a callback function. Sync ajax crashes/locks up the browsers.

Here are some ajax examples based on xhr2 witch should be the standard now. it's supported by all new browsers including ie10,android & ios.

Simple get ajax function

//url,callback
function ajax(a,b,c){
 c=new XMLHttpRequest;
 c.onload=b;
 c.open('get',a);
 c.send()
}

Usage

ajax(url,callback);

How to access the return value inside the callback

function callback(e){
 e.target.response
 this.response
 c.response// if inline inside the ajax function.
}

Complex ajax function

//url,callback,type,FormData,uploadFunc,downloadFunc
function ajax(a,b,e,d,f,g,c){
 c=new XMLHttpRequest;
 !f||(c.upload.onprogress=f);
 !g||(c.onprogress=g);
 c.onload=b;
 c.open(e||'get',a);
 c.send(d||null)
}

Usage

var fd=new FormData();
fd.append('act','get_conv_data');
fd.append('click_id',click_id);
fd.append('server',server);
ajax(ajaxUrl,callback,'post',fd);

function callback(){
 //when the request finishes do stuff with the response.
 alert(this.response)
}

or

html

<form id="form">
 <input name="server" value="serverBLABLA">
 <input name="click_id" value="click_idBLABLA">
 <input name="act" value="get_conv_data">
</form>

js

ajax(ajaxUrl,callback,'post',new FormData(document.getElementById('form')));

function callback(){
 //when the request finishes do stuff with the response.
 alert(this.response)
}

More info about this function

https://stackoverflow.com/a/18309057/2450730

Community
  • 1
  • 1
cocco
  • 16,442
  • 7
  • 62
  • 77
0

It is because of the Javascript's asynchronous execution feature. Before the data comes and is assigned in your "tmp" variable, the return is executed. So you are getting blank data. Try below solution. Hope this will work for you :

    function get_conv_data(click_id,server) {  
    var tmp; 
    new Ajax.Request(ajaxUrl, 
    {   
    method: 'post',
    parameters: {
        'act': 'get_conv_data',                
        'click_id': click_id,
        'server':server              
    },
    onSuccess: function(res) 
    {
    alert(click_id+'xx'+server);
    tmp=res.responseText; 
    return tmp;
    }
    }); 
    }