Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save data from AJAX request in variable (jQuery)

Tags:

jquery

ajax

How can i get the data from a AJAX request saved in a variable with jQuery?

like image 811
Mikkel Avatar asked Jan 06 '10 16:01

Mikkel


3 Answers

How About This?

For Json Data:

var BaseConfig=$.ajax({
                       async:false,
                       url:'./config.php',
                       type:'get',
                       data:{'GetConfig':'YES'},
                       dataType:"JSON"
                       }).responseJSON;

And For Text Data:

var BaseConfig=$.ajax({
                       async:false,
                       url:'./config.php',
                       type:'get',
                       data:{'GetConfig':'YES'},
                       dataType:"TEXT"
                       }).responseText;
like image 83
MSS Avatar answered Oct 14 '22 11:10

MSS


var myvar;
$.ajax({
    url: 'mything.php',
    success: function(data) {
        myvar = data;
    }
});
like image 36
PetersenDidIt Avatar answered Oct 14 '22 11:10

PetersenDidIt


use this

var myvar='';
$.ajax({
    type:'post',
    url: 'your url',
    dataType:'text',
    success: function(data) {
        useReturnData(data);
    }
});

function useReturnData(data){
    myvar = data;
    console.log(myvar);
};
like image 32
sdf Avatar answered Oct 14 '22 13:10

sdf