Posted By

killeralienmonk on 03/08/13


Tagged

AjaxJqueyPHPtip


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

3polars


Cross domain AJAX querying with jQuery


 / Published in: PHP
 

URL: http://jquery-howto.blogspot.ca/2009/04/cross-domain-ajax-querying-with-jquery.html

JUST TO LET YOU KNOW THIS SNIPPET IS NOT MINE THE LINK IS IN THE URL SECTION.

How to make AJAX queries to domains other then yours. Basically how to achieve cross domain scripting with jQuery. The technique will help you resolve the access to restricted uri denied" code: 101" problem.

Using this method for cross site scripting you will be able to:

  1. Make AJAX queries to any domain even those that differ from your own.
  2. Use any of $.get(), $.post(), $.ajax(), $getScript(), etc. jQuery AJAX functions as your query method.

You will need to put a proxy to between you and the rest of the world. This cross domain querying solution works because you actually loading the content from your own domain. You request the URL and the proxy script on your server actually loading the content from the internet and passing it over to you.

  1. <?php
  2. // Set your return content type
  3. header('Content-type: application/xml');
  4.  
  5. // Website url to open
  6. $daurl = 'http://feeds.feedburner.com/jQueryHowto';
  7.  
  8. // Get that website's content
  9. $handle = fopen($daurl, "r");
  10.  
  11. // If there is something, read and return
  12. if ($handle) {
  13. while (!feof($handle)) {
  14. $buffer = fgets($handle, 4096);
  15. echo $buffer;
  16. }
  17. fclose($handle);
  18. }
  19. ?>
  20.  
  21. // I named the file proxy.php and made my AJAX request to this url. Here is a jQuery code example:
  22.  
  23. <script>
  24. $("#rssFeeds").load("path/to/proxy.php", function(){
  25. // Some callback functions
  26. });
  27. </script>

Report this snippet  

You need to login to post a comment.