[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

getting simple JSON query to work (with ajax)

Posted on 2011-04-20
5
Medium Priority
?
892 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.
0
Comment
Question by:tomermes
  • 4
5 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 35434497
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/
0
 

Author Comment

by:tomermes
ID: 35435122
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
0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 35436264
ok, the fastest workaround is to use a proxy.

$.getJSON("path/to/proxy.php?url=" + encodeURIComponent("http://where.yahooapis.com/geocode?location=San+Francisco,+CA&flags=J&appid=yourappid"), function(data) {
     // parse the data now
});

with the following php proxy (light) :
<?PHP
   echo file_get_contents($_GET['url']);
?>

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 35436456
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/
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35437507
Thanks for the points!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question