문제

I'd like to do the following: grab news from several sites, parse their content 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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top