We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

getting simple JSON query to work (with ajax)

tomermes
tomermes asked
on
Medium Priority
921 Views
Last Modified: 2013-11-19
I was reading a lot of examples in order to execute a simple JSON query from yahoo
place finder api but I still can't get anything that works.

I had success with making it work with a simple php format but I don't want my page to refresh so now I must try and do this in ajax.

putting it simple: I need to retrieve data from:

    http://where.yahooapis.com/geocode?location=San+Francisco,+CA&flags=J&appid=yourappid

(not for the general case but enough for me understanding how to do it)

which is in the JSON format and parse the "uzip" field (any field would do for me to understand).

please help me with the lines I'm searching for- couldn't find the exact thing- thanks a lot.
Comment
Watch Question

leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
You need to use JSONP and not a basic ajax call :

Using jQuery it's faster :
$.getJSON("http://where.yahooapis.com/geocode?location=San+Francisco,+CA&flags=J&appid=yourappid", function(data) {
     // parse the data now
});

Open in new window


Else with simple javascript (note the parameter callback at the end of your URL) :

var cb = function(data) {
// parse the data now
};

var script = document.createElement('script');
script.src = "http://where.yahooapis.com/geocode?location=San+Francisco,+CA&flags=J&appid=yourappid&callback=cb";
document.body.appendChild(script);

Open in new window


If you need more help, don't hesitate.

Some ressources :

http://stackoverflow.com/questions/3892411/how-can-i-write-this-yql-statement-for-jquery-jsonp
http://projects.ischool.washington.edu/tabrooks/343INFOAutumn09/JSONP/jsonpJqueryYQL.htm
http://james.padolsey.com/javascript/using-yql-with-jsonp/
http://www.ibm.com/developerworks/web/library/wa-aj-jsonp2/

Author

Commented:
i tried using the callback but it wouldn'y work.
if you try the link as you put it with the callback=cb you'll see
that yahoo don't had the cb(...) around the respond as expected
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
check the provided links too (the one using of YQL)

else more info about using a proxy here for example : http://james.padolsey.com/javascript/cross-domain-requests-with-jsonp-safe/
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Thanks for the points!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.