Trouble with JQuery Ajax reading JSON -
Trouble with JQuery Ajax reading JSON -
i'm using jquery seek read json , maintain ending in error handler. when inspect dom see "error" under statustext no more details. i've tried saving local file , have added beforesend section address potential mime problems based on post. when open url in browser generate valid json.
$.ajax({ type : "get", url : "http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true", beforesend : function(x) { if (x && x.overridemimetype) {x.overridemimetype("application/j-son;charset=utf-8");}}, datatype : "application/json", success : function(data) { alert("success" + data)}, error : function(y) {alert("failure" + y.statustext);}
});
there nil wrong code. problem occurring because trying perform cross-domain ajax request. however, not lost because google provides google maps javascript api v3 services - client-side geocoding api.
the next total code sample (tested , working) give access json object require. in example, alert box display "williamsburg, ny, usa" - first formatted_address.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> $(document).ready(function() { var geocoder = new google.maps.geocoder(); var input = "40.714224,-73.961452"; var latlngstr = input.split(",", 2); var lat = parsefloat(latlngstr[0]); var lng = parsefloat(latlngstr[1]); var latlng = new google.maps.latlng(lat, lng); geocoder.geocode({ 'latlng': latlng }, function (results, status) { if (status == google.maps.geocoderstatus.ok) { if (results[1]) { alert(results[1].formatted_address); } } else { alert("geocoder failed due to: " + status); } }); }); </script>
jquery ajax
Comments
Post a Comment