0

here is my example

var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + area.getCenter().lat + "," + area.getCenter().lng + "&sensor=false";

$.getJSON(url, function (data) {
    alert("inside json");
    //console.log(data);

    $.each(data.results[0], function (i, inside) {
        console.log(i);

    });

    //var addr = data.results[0].geometry.location.lat;

});

now i realized that the call function doesn't fire at all....i read other post here and they say that it happens when JSON is not in valid format but here i am getting JSON from Google API so i hope its in valid format.... JSON is here

http://maps.googleapis.com/maps/api/geocode/json?latlng=27.88,78.08&sensor=false

can you please give your comments on this? or may be i am making some mistakes

Arturs
  • 1,258
  • 5
  • 21
  • 28
Wanna_be_Coder
  • 29
  • 1
  • 1
  • 10
  • possible duplicate of [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – John Dvorak Sep 01 '13 at 13:56

2 Answers2

1

Maybe try a full ajax call:

    $.ajax({
        url: "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + area.getCenter().lat + "," + area.getCenter().lng + "&sensor=false",
        data: {},
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "JSON",
        timeout: 10000,
        success: function (Result) {
            for (var i = 0; i < Result.d.length; i++) {
                element = Result.d[i];
                console.log(element);
            };

        },
        error: function (xhr, status) {
            alert(status + " - " + xhr.responseText);
        }
    });
Jacques Bronkhorst
  • 1,685
  • 6
  • 34
  • 64
  • hi i tried this but nothing happened. I checked on firebug and the success: function (Result) doesn't goes inside..so it is not success....and then it goes to error function but doesn't show any alerts...any idea what should be next – Wanna_be_Coder Sep 01 '13 at 14:14
  • Try changing the URL directly to : url: "http://maps.googleapis.com/maps/api/geocode/json?latlng=27.88,78.08&sensor=false". This should give you an indication if area.getCenter().lat and area.getCenter().lng are working correctly. This is just for testing, and what error does your console report? – Jacques Bronkhorst Sep 01 '13 at 14:19
  • Hi yes i changed it to direct url but again the same thing..so it means getCenter() is working. and surprisingly firebug doesn't throw any error , it silently comes out of error: function (xhr, status) { alert(status + " - " + xhr.responseText); } without even touching the inner statement i.e alert(status + " - " + xhr.responseText); so my conclusion is firebug goes to success, but doesn't execute inner statement may be because its not success...again it checks for error but doesn't go inside..so it is bypassing everything..ok...so what should i do next..thanks – Wanna_be_Coder Sep 01 '13 at 14:31
  • Try a different browser, and get fiddler: http://fiddler2.com/, this will help you understand the request being generated. – Jacques Bronkhorst Sep 01 '13 at 14:38
  • Looking at this fiddle: http://jsfiddle.net/Fumunchu/RdtMg/ it seems that there is an authorisation issue with the maps API (I get 405) – Jacques Bronkhorst Sep 01 '13 at 14:47
  • Hey i think its solved. I created a separate function and calling getJSOn from it. it works..but still couldn't identify why it wasn't working in the normal program flow..anyhow thanks a lot – Wanna_be_Coder Sep 01 '13 at 15:09
  • All that counts at the end of the day. – Jacques Bronkhorst Sep 01 '13 at 15:14
0

can you add it to

$(document).ready(function(){

});

for example:

  $(document).ready(function () {
      var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + area.getCenter().lat + "," + area.getCenter().lng + "&sensor=false";

      $.getJSON(url, function (data) {
          alert("inside json");
          //console.log(data);

          $.each(data.results[0], function (i, inside) {
              console.log(i);

          });

          //var addr = data.results[0].geometry.location.lat;

      });

  });
Arturs
  • 1,258
  • 5
  • 21
  • 28
Liad Livnat
  • 7,435
  • 16
  • 60
  • 98