14

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

1
  • Are you asking how to use jQuery's ajax function? All of that information is available in the doc: docs.jquery.com/Ajax If you're having specific troubles, we'll need to see some example code. Commented Jan 6, 2010 at 16:52

3 Answers 3

21
var myvar;
$.ajax({
    url: 'mything.php',
    success: function(data) {
        myvar = data;
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

its not working in my case. I need to store xml returned by soap api in variable. but its not storing
I need to access this variable outside this ajax request
Unless you set your ajax call to async:false, your var won't hold that data by the time you use it.
11

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);
};

1 Comment

This worked for me, thanks! I'm not sure why this is such a problem. I feel like I've assigned the variable directly in the async function but this time it just wasn't working. You helped!
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.