开发者

Get content from another site

I saw here similar questions but I didn't find any answer about Javascript.

I'm building a web site (lets call it 'A'), and I want to get content from another web site ('B') that requires a user-name and password. I want a function at my site that get the content from a certain page at B. I'm always login manually to site B at my computer so I don't need t开发者_如何学JAVAhe function to do the login (so I link it directly to the correct page of the info at B). A and B are not in the same domain. Is there a way in Javascript to get content from B?


You need a cross-doman AJAX call (normally prevented by the same origin policy). jQuery has a handy helper function for this that will return JSON data called $.getJson()

$.getJSON('http://otherdomain/ajax/test.json', function(data) {
  if (undefined != data) {
    console.log(data);
  }
});

This exploits a technique known as JSONP, which writes Javascript directly into the document to make the request (instead of using the XMLHttpRequest object), bypassing the same origin policy.


What I like to do is use YQL (Yahoo Query Language) It's like an api for api's. I get whatever html I want using selectors and process that. For example, I can grab all the images from this wikipedia link using a query like SELECT * FROM html WHERE url="http://en.wikipedia.org/wiki/List_of_United_States_National_Parks_by_state" AND xpath="//img" and then processing the returned XML/JSON

you can test queries HERE

and see an example of grabbing and processing the images in this Fiddle


No there is no way to get content from external B page using pure javascript, but you can try to use php curl or file_get_contents

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜