ajlisowski Posted August 29, 2011 Share Posted August 29, 2011 So, I am making a jquery ajax call to a restful application. The call generates a url of: http://geoapi.fwix.com/places.json?api_key=25645fa7e150&lat=39.8062&lng=-86.1407&range=2 Which if you go to that url you will see it properly generates JSON. However, looking at firebug, the call fails to get a response from the server. my jquery code is as follows: var latitude=$('#location_lat').val(); var longitude=$('#location_long').val(); var range=obj.range; var data='api_key='+obj.fwixAPI; data=data+'&lat='+latitude; data=data+'&lng='+longitude; data=data+'&range='+range; data=data+'&page_size='+10; $.ajax({ type: "GET", url: 'http://geoapi.fwix.com/places.json', data: data, dataType: 'json', success: function(msg){ system_obj.debug(msg); } }); Since the URL generated by the request is properly outputting JSON, I can not figure out why it is failing, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/245986-ajax-call-issue/ Share on other sites More sharing options...
nogray Posted August 30, 2011 Share Posted August 30, 2011 You can't request a different domain (including subdomains) using ajax. Security issues. Quote Link to comment https://forums.phpfreaks.com/topic/245986-ajax-call-issue/#findComment-1263386 Share on other sites More sharing options...
ajlisowski Posted August 30, 2011 Author Share Posted August 30, 2011 so then what is the proper way to retrieve data from an external source using javascript? Quote Link to comment https://forums.phpfreaks.com/topic/245986-ajax-call-issue/#findComment-1263480 Share on other sites More sharing options...
trq Posted August 30, 2011 Share Posted August 30, 2011 You can use jsonp if the api provides it. The one your using doesn't appear to. Quote Link to comment https://forums.phpfreaks.com/topic/245986-ajax-call-issue/#findComment-1263504 Share on other sites More sharing options...
ajlisowski Posted August 30, 2011 Author Share Posted August 30, 2011 Yeah, Im assuming if it did, places.jsonp would be the correct format. That explains why when I tried changing my request type to jsonp, I did get a response, but it couldnt parse it. Right now my solution is to make a proxy php page which simply routes the request and displays the result. This is working fine, but it brings up a concern. Some APIs have a usage limitation based on IP. If I could send it directly via javascript, the IP would be that of the client. If i use the re-route through my php server, the IP will be that of the server and reach its limit much, much quicker. With the particular service I am using, it DOES have a 5000 query limit per user. But I believe that amount is reached via a unique user id passed with the request and not via IP. So I should be fine there, because I can just send the user ID or generate a random one for a guest and send that. Quote Link to comment https://forums.phpfreaks.com/topic/245986-ajax-call-issue/#findComment-1263513 Share on other sites More sharing options...
ajlisowski Posted August 30, 2011 Author Share Posted August 30, 2011 Actually, the service does accept jsonp! I just had to add jsonp=return function to the end of the call and it works fine! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/245986-ajax-call-issue/#findComment-1263535 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.