最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get content from another site - Stack Overflow

programmeradmin1浏览0评论

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 puter so I don't need the 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?

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 puter so I don't need the 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?

Share Improve this question edited Jun 18, 2011 at 12:34 insumity 5,4718 gold badges38 silver badges65 bronze badges asked Jun 18, 2011 at 12:08 WitterquickWitterquick 6,1504 gold badges28 silver badges54 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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/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)

  1. 暂无评论