0

I want to know how to pass value from ajax function to another ajax I've tried this code but It does not work. Anyone know how to do it the right way? Thanks!

    $("#button").click(function(){
    var passedValue = '';
    $.ajax({
        url://
        type://
        success:function(data){
            if(typeof==="object"){
            for(var i=0;i<data.length;i++){
                passedValue = data[i]["passedThis"];
            }
        }
    }
    });
    $.ajax({
        data: { receivedPassed: passedValue;}
    });
});
Brained Washed
  • 701
  • 4
  • 9
  • 20
  • 2
    My thoughts are that ajax calls are by default ran `asynchronously` meaning that they will all fire at the same time of invocation without waiting on the next. Because of this, `passedValue` will be null. if you add `async:false;` to the first ajax call, it shuold work. – Ohgodwhy Sep 16 '12 at 16:31
  • But, it will give the appearance that your webpage has locked up. – pickypg Sep 16 '12 at 16:32
  • 1
    2 issues. Your variable is declared inside the click function, so it'll be out of scope when you need it later. Also, the ajax calls are asynchronous, so the second ajax call will try to read the variable before it's ready. – Lee Taylor Sep 16 '12 at 16:33
  • @LeeTaylor: Just a minor note: it won't be out of scope! JavaScript has function scope, and the variable will remain valid in the nested functions. –  Sep 16 '12 at 16:34
  • 1
    @qwzjk Ah, yes. I couldn't tell because of the tabbing/braces. – Lee Taylor Sep 16 '12 at 16:36
  • @LeeTaylor: Ah, sorry, I can see how that's an easy mistake to make –  Sep 16 '12 at 16:38

2 Answers2

2

Those requests are asynchronous, so passedValue isn't ready by the time the second one is sent. You'll probably want to send them in order:

$("#button").click(function() {
    $.ajax({
        url: //
        type: //
        success: function(data) {
            var passedValue;

            if(typeof (?) === "object") {
                for(var i = 0; i < data.length; i++) {
                    passedValue = data[i]["passedThis"];
                }

                $.ajax({
                    data: { receivedPassed: passedValue }
                });
            }
        }
    });
});

Also, you're overwriting passedValue each time through that loop. Did you mean += instead of =, or to use an array, or something?

  • @BrainedWashed: Well, you had `typeof === "object"` in your original code. I take it you took out the actual variable? I added the `(?)` to make it more obvious that it's a placeholder. –  Sep 16 '12 at 16:41
  • I have question what if I will make that $.ajax like function receivedValue()? can I still get the passedValue? – Brained Washed Sep 16 '12 at 18:29
  • @BrainedWashed: Yup - as long as `passedValue` is declared inside that scope and the other callback is somewhere within the same scope too. –  Sep 16 '12 at 18:54
  • this is what I mean i'm really having hard time please help [link](http://stackoverflow.com/questions/12449655/how-can-i-pass-this-specific-variable-im-using-jquery-ajax) :) – Brained Washed Sep 16 '12 at 18:57
1

Chain the AJAX calls using .then(), and filter data from the first call to the second using .pipe():

$("#button").click(function() {
    $.ajax({
      url: //
      type: //
    }).pipe(function (data) {
      var passedValue = [];
      for (var i = 0; i < data.length; i++) {
        passedValue[i] = data[i]["passedThis"];
      }
      return passedValue;
    }).then(function (receivedPassed) {
      $.ajax({
        data: { receivedPassed: passedValue }
      }
    }
});

Also note that the .success() callback will be deprecated in jQuery 1.8:

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8.

João Silva
  • 89,303
  • 29
  • 152
  • 158
  • It would probably be a good idea for them to move that deprecation notice higher up the page –  Sep 16 '12 at 18:57