jQuery: getting/parsing content from different sites
I'd like to do the following: grab news from several sites, parse their co开发者_开发技巧ntent using jQuery selectors and show them on one page.
How could this be done with jQuery?
Thanks.
For security reason's JavaScript (and thus jQuery) AJAX methods can only retrieve data from URL's on the same domain as your site.
There are some work-around's however. You could use a server-side script to download remote content for you - think of it like a proxy. Alternatively you could look into JSONP, but the remote site needs to provide it.
It can't with pure jQuery or JavaScript because you can't fetch content from domains different from the one the script is running from. This is a security measure to prevent Cross Site Scripting
But see a possible solution here: Cross site scripting(XSS)
Unless your news is coming from sites that explicitly provide a mechanism for fetching content along the lines of what you want, you can't do this from the client (the browser, that is). You can fetch the content from your server, however, and then hand it over to your client in as raw a state as you like.
As far as javascript is concerned, you can't get content off other pages, unless they are sharing it explicitly using an API.
What you can do, is use cURL on the server side to get data from other websites and then manipulate that - server side and serve it.
Note : Taking content that belongs to others - manipulating it without their consent and presenting as your own is invitation to trouble.
Contrary to other replies, script block sources are not blocked from cross domain access, so if you dynamically add a script block to the header of your page and have the resultant output from the site created as a callback (wrap it in a function call basically, and handle taht function in your own code) then you can access content on other domains - the flip side is that you need the site to present you the data in a callback, otherwise it doesnt work.
if the websites you are trying to grab the news from supports rss feed then you can use jquery plugins like jFeed to retrieve the rss and you can then just display it on your own website.
although, i would recommend you setup a webservice (in asp.net, php, etc) that gathers rss or scrape meta tags in the websites you want and then use jquery.get() to retrieve all news
精彩评论