2

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.

Mat Richardson
  • 3,576
  • 4
  • 31
  • 56
  • 1
    This type of request requires either an external webserver that supports CORS, or a proxy of some sort that allows you to get around the same-origin policy such as YQL or a server-side script. – Kevin B May 03 '12 at 20:18

3 Answers3

3

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;
?>
Robot Woods
  • 5,677
  • 2
  • 21
  • 30
  • And is the php scripting something that is easy enough to carry out? I'm not experienced in php at all, but sure I could pick it up quickly enough.. – Mat Richardson May 03 '12 at 20:21
  • it can be very simple depending on your needs, I added a sample script, but you may need to send parameters along with your AJAX request, so you should look at `$_POST`, `$_GET`, and `$_REQUEST` for how you would do so – Robot Woods May 03 '12 at 20:26
  • Oh, but some sites check URL so prohibit other sites from accessing them, so it's not a perfect solution – Robot Woods May 03 '12 at 20:30
0

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

Community
  • 1
  • 1
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
0

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.

Carlo Moretti
  • 2,213
  • 2
  • 27
  • 41