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 badges3 Answers
Reset to default 5You 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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745180144a4615381.html
评论列表(0条)