0

I am trying to access variable 'dimensions' in my ajax response but not able to get it. I dont want to make this variable global. Following is my code

  $(document).ready(function(){
  $('#submittext').click(function(){
   var dimensions;
   $.ajax({
     type: "GET",
     url: "bin/getcontentsize.php",
     data: findContentsize,
     success: function(response){
       //want to access dimensions here to assign response and some calculation(but not able to access it)
     }
     });
   //so i can use here
  });
  });
5
  • What do you mean you can't access it? Does it produce an error, wrong value, etc ... Commented Nov 7, 2011 at 16:18
  • 8
    You should be able to access dimensions there fine. It will however be 'undefined' as you've never initialised it. Commented Nov 7, 2011 at 16:19
  • @samjudson yes it is saying not defined Commented Nov 7, 2011 at 16:20
  • I'd rather guess your problem is that the code below your $.ajax() call is executed immediately and dimensions will still be undefined. Your success callback is executed asynchronously, i.e. at a later time, so you should trigger whatever you're doing with dimensions from there Commented Nov 7, 2011 at 16:21
  • 1
    You can't use the variable where you have labelled "//so i can use here". It won't be filled when you get to that line of code. It will only be filled after the ajax success callback is called. See my answer here: stackoverflow.com/questions/7887284/… Commented Nov 7, 2011 at 16:22

4 Answers 4

7

In this case you can access the dimensions variable from both the ajax call back and the code immediately after starting the ajax request. The variable is accessible in both of these contexts.

What is most likely causing the problem though is the timing. The success method will run asynchronously after the ajax request is completed. It's best to view this as executing later. However the code immediately after the $.ajax call will execute immediately. Hence you won't see any effects from the success handler on the dimensions variable when it runs.

If there is code you want to run with the value of dimensions as calculated by the success method you need to call that code from the success callback. For example

$('#submittext').click(function(){

   var handleNewDimensions = function (dimensions) {
     // Code that used to be after the $.ajax line
   }

   $.ajax({
     type: "GET",
     url: "bin/getcontentsize.php",
     data: findContentsize,
     success: function(response){
       var dimensions = doTheCalculation(...);

       // Call the code which needs to deal with the new dimensions
       handleNewDimensions(dimensions);
     }
   });
Sign up to request clarification or add additional context in comments.

Comments

2

Problem when you run it.

$(document).ready(function(){
    $('#submittext').click(function(){
        var dimensions="1";
        $.ajax({
            type: "GET",
            url: "bin/getcontentsize.php",
            data: findContentsize,
            success: function(response){
                dimensions = "2";
            }
        });
        //!!!!Attention
        alert(dimensions);  // result is "1", not "2"
    });
});

First, your code already ran. After that, your $.ajax starts to run.

Comments

1

Assign the dimensions variable the value, and test it again:

var dimensions="sample";

Comments

1

This should work:

  $(document).ready(function(){
  $('#submittext').click(function(){
   var dimensions = 1;
   $.ajax({
     type: "GET",
     url: "bin/getcontentsize.php",
     data: findContentsize,
     success: function(response){
       alert(dimensions);
     }
     });
   //so i can use here
  });
  });

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.