-1

I have this ajax request call to a json supported link (url1):

$.ajax({
     type : "GET",
     dataType : "json",
     url : url1,
     success: function (data) {
            c1lat1 = [(data[0].boundingbox[0]),(data[0].boundingbox[2])];
            alert(c1lat1);
     }
});

This works perfectly fine, and the alert does give the number I want. However, if I try this:

$.ajax({
     type : "GET",
     dataType : "json",
     url : url1,
     success: function (data) {
            c1lat1 = [(data[0].boundingbox[0]),(data[0].boundingbox[2])];
     }
});
alert(c1lat1);

It gives me an empty alert box, and therefore it looks like it's not saving the value into the variable into the request.

How can I get around this as I need that value retrieved?

Thanks!

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
tj56
  • 162
  • 3
  • 12
  • I have initialized it just before the ajax call. – tj56 Jun 17 '14 at 08:25
  • 1
    it's about synchronicity not scope. The ajax call by default is asynchronous. That's it, the `success` callback is **always** invoked after your `alert` – zerkms Jun 17 '14 at 08:25
  • `c1lat1 ` this variable name drives me crazy... – merlin Jun 17 '14 at 08:26
  • With scope correct, you have to look at how it runs... sends the Ajax call, then calls the alert... doesn't wait for ajax to finish. –  Jun 17 '14 at 08:26
  • How can I get it wait then? – tj56 Jun 17 '14 at 08:30
  • 1
    @Jeremy Miller: it has nothing to do with scopes. Please don't spread your personal misunderstandings. Thank you. – zerkms Jun 17 '14 at 08:30
  • 1
    @JeremyMiller — You're confusing definition (which doesn't matter for scope) with declaration (which does). – Quentin Jun 17 '14 at 08:31
  • @Quentin Thank you. I have removed the fallacious comment. –  Jun 17 '14 at 08:34
  • @Jeremy Miller: I have no idea what you're talking about. I'm making SO a better place by pointing people what they are talking is wrong. It makes sense to teach people instead of helping spreading misunderstandings. Especially when you were told twice you're wrong. – zerkms Jun 19 '14 at 02:56

2 Answers2

2

Since ajax is async by default, you have two options-

  1. Alert the variable in the callback. You must proceed after you get the result in the callback-

    $.ajax({
      type : "GET",
      dataType : "json",
      url : url1,
      success: function (data) {
          c1lat1 = [(data[0].boundingbox[0]),(data[0].boundingbox[2])];
          alert(c1lat1);
      }
    });
    
  2. Make the call synchronous using async: false, this will make your call synchronous, and the control will come after $.ajax();

    $.ajax({
      type : "GET",
      async: false,
      dataType : "json",
      url : url1,
      success: function (data) {
          c1lat1 = [(data[0].boundingbox[0]),(data[0].boundingbox[2])];
      }
    });
    alert(c1lat1);
    

Reference

Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
  • 1
    from documentation: `As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated` – Justinas Jun 17 '14 at 08:30
  • Straight to the point. +1 Please add an explanation why your answer works, though. – Cerbrus Jun 17 '14 at 08:30
  • @Justinas — So what? The `async: false` isn't being used with jqXHR. (SJAX is still horrible though). – Quentin Jun 17 '14 at 08:30
  • Thanks, the second one worked! But is there a compatibility or deprecation issue with it? – tj56 Jun 17 '14 at 08:34
  • Only with jqXHR, you can use this with your call :) PS, accept the answer if worked, so the discussion may close here. – Sahil Mittal Jun 17 '14 at 08:37
-2

1) it's scope problems. c1lat1 inside success is local variable. Define it globaly.

2) success is async, so your alert happens before response from server, so no variable is set yet. move alert to success

Justinas
  • 41,402
  • 5
  • 66
  • 96
  • 1
    It's not local. Why do you think it is? – zerkms Jun 17 '14 at 08:27
  • 1
    @zerkms because i don't see it's declaration before entering Ajax – Justinas Jun 17 '14 at 08:28
  • @Justinas — JavaScript doesn't work like that. It isn't declared inside the function, so the scope must be from outside the function. – Quentin Jun 17 '14 at 08:29
  • 1
    @Justinas: so? There is no declaration indeed. Why do you think it's local? http://jsfiddle.net/mGNQ3/ - here is an example that might help you get that what you're stating is incorrect – zerkms Jun 17 '14 at 08:29
  • Try this: `(function (){x = 1;})(); console.log(x);` If `x` isn't explicitly declared to `a`'s scope, it's global. Scope is not the issue here. – Cerbrus Jun 17 '14 at 08:32
  • It's my bad I haven't included the declaration; but I have indeed initialized the variable before the call. – tj56 Jun 17 '14 at 08:34