Cross domain AJAX querying with jQuery


/ Published in: PHP
Save to your folder(s)

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

<p>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.</p>

<p>Using this method for cross site scripting you will be able to:</p>

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

<p>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.</p>


Copy this code and paste it in your HTML
  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>

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.