Request external website data using jQuery ajax

丶灬走出姿态 提交于 2019-12-06 09:12:12

问题


I've tried reading up about this and not really sure where to start, so hoping somebody will be able to point me in the right direction.

Basically I'm trying to use jQuery ajax to read an external website and retrieve a list of links from it. No particular reason as such at the moment, just wanted to see if I could challenge myself by doing it.

In doing so I've read up a little about the Same Origin Policy and understand it (sort of) but could do with some pointers.

Is this possible to do? I've been looking at the scrabble points calculator used by Kate Spanos, for example, and her jQuery code contains some ajax which appears to check dictionary websites and work on some of the output.

Could somebody point me in the right direction, or am I barking up the wrong tree and is it basically impossible to do without some other technical knowledge.

Thanks, Mat

PS I am a 'noob', so please be as gentle as possible. We all have to start somewhere with this stuff so please don't shoot me down...Thanks in advance.


回答1:


You should look into JSONP, or more likely use some sort of intermediary, like a PHP script (so same origin) that uses cURL or file_get_contents to access the third party site

for instance:

<?php
$file=file_get_contents('http://some_domain_not_yours.com/somefile');
echo $file;
?>



回答2:


try referring these , hope it help

jsonp with jquery

http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

http://api.jquery.com/jQuery.getJSON/#jsonp




回答3:


you should do this via PHP i.e. loading via PHP include the external site and than parse it in your PHP.

You cannot do this via jQuery, basically you can't make a client retrive remote content without a server side to filter it. If a client could access freely remote content you won't have any control on data accesses for the SOP you always need a server in between to guarantee the content management and filtering, this can be either your server or a remote server (like an API provider). To assure this, you can only share JSON objects cross domains, JSON objects are objects created via PHP (for example) so you can't get a JSON object without a server script. The other way (your server is between) is you make a server retriving remote content and then givin' it to your client in any format you like.



来源:https://stackoverflow.com/questions/10438683/request-external-website-data-using-jquery-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!