getting simple JSON query to work (with ajax)

Posted on 2011-04-20
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:,+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.
Question by:tomermes
    LVL 81

    Expert Comment

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

    Using jQuery it's faster :
    $.getJSON(",+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 = ",+CA&flags=J&appid=yourappid&callback=cb";

    Open in new window

    If you need more help, don't hesitate.

    Some ressources :

    Author Comment

    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
    LVL 81

    Accepted Solution

    ok, the fastest workaround is to use a proxy.

    $.getJSON("path/to/proxy.php?url=" + encodeURIComponent(",+CA&flags=J&appid=yourappid"), function(data) {
         // parse the data now

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

    Open in new window

    LVL 81

    Expert Comment

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

    else more info about using a proxy here for example :
    LVL 81

    Expert Comment

    Thanks for the points!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now