Getting data from other sites using javascript [closed]

I want to to get data within divs from other websites. How can I do that using JavaScript?

Answers:

Answer

Due to cross domain restrictions you can't access the html directly using AJAX.

You can however use Yahoo YQL to select the part(s) of a page you want and have that html returned within jsonp data.

Example returning list of questions on main page of stackoverflow

var url='http://query.yahooapis.com/v1/public/yql?q=select * from html where url=\'http://stackoverflow.com/\' and xpath=\'//div[@id="question-mini-list"]//h3//a\'&format=json&callback=?';


$.getJSON( url, function(data){
    $.each(data.query.results.a, function(){       
        $('body').append('<div><a href="http://stackoverflow.com'+this.href +'">'+this.content+'</a></div>')          
     })
})

DEMO: http://jsfiddle.net/NTUx5/

YQL docs: http://developer.yahoo.com/yql/guide/index.html

Answer

You will either have to use the JSONP technique, as jungalist suggested above; or get your AJAX method to talk to a local PHP script which uses cURL:

jQuery:

$('#result').load('path/to/script.php');

PHP:

$ch = curl_init("http://example.com");
curl_exec($ch);
curl_close($ch);

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.