Saturday, 20 April 2013

Getting cross domain JSON Data by ajax request

Their is two way to get data from other domain.

1. By using YQL Yahoo Service. Here is example :-

function buildQuery(term) {
    return "select * from json where url = 'http://airportcode.riobard.com/search?fmt=JSON&q=" + term + "'";
}

$.ajax({
        url: 'http://query.yahooapis.com/v1/public/yql',
        data: {
            q: buildQuery(YourURL),
            format: "json"
        },
        dataType: "jsonp",
        success: function(data) {
            alert(JSON.stringify(data));
        },
        error: function (data) {
            consol.log(data);
        }
});


2. By using $.getJSON Here is example-


$.getJSON("http://kalpa.freeoda.com/news/news_feed.php?callback=?", null, function (data) {
            alert(JSON.stringify(data));
});




No comments :

Post a Comment