1

using this code in the drupal block:

<script type="text/javascript">
$('#block-block-16').load('http://nashmir.kz');
</script>

but it won't load content from domain nashmir.kz. With local files it works well. Can somebody throw me a working example on how to load content from another domain?

  • Which version of Drupal are you using and which format type have you choose when you added this code to a block ? – yvan Sep 21 '11 at 9:22
  • Ofcourse PHP, drupal version is 6.22 – Alexander Kim Sep 21 '11 at 9:56
4

You can use a local PHP script to pull the external data. Then use AJAX to show it in a block.

      // ajax call to a php script
      $.post("/sites/scripts/my_script.php",
        { "post_parameter" : "post_value" },
        function(data){              
          // put the result into a div
          $('#result').html(data);
        }
      ); 

Then again if you only want to display an external website, you can use an <iframe>.

3

Cross domain can not be invoked with .load(). It's a security problem. Ajax cross domain is XSS. http://en.wikipedia.org/wiki/Same_origin_policy If the other site has some scripts, it can mess with your page basically.

What you need is jsonp or cors https://developer.mozilla.org/en/HTTP_access_control

cheers T

  • can you throw me an example? – Alexander Kim Sep 21 '11 at 10:12
0

There is a little hack with php. And it works not only with Google, but with any website you don't control and can't add Access-Control-Allow-Origin *

We need to create PHP-file (ex. getContentFromUrl.php) on our webserver and make a little trick.

PHP

<?php

$ext_url = $_POST['ext_url'];

echo file_get_contents($ext_url);

?>

JS

$.ajax({
    method: 'POST',
    url: 'getContentFromUrl.php', // link to your PHP file
    data: {
        // url where our server will send request which can't be done by AJAX
        'ext_url': 'https://stackoverflow.com/questions/6114436/access-control-allow-origin-error-sending-a-jquery-post-to-google-apis'
    },
    success: function(data) {
        // we can find any data on external url, cause we've got all page
        var $h1 = $(data).find('h1').html();

        $('h1').val($h1);
    },
    error:function() {
        console.log('Error');
    }
});

How it works:

  1. Your browser with the help of JS will send request to your server
  2. Your server will send request to any other server and get reply from another server (any website)
  3. Your server will send this reply to your JS

And we can make events onClick, put this event on some button. Hope this will help!

  • Thanks for providing an answer. I think that you should provide a big security warning because this basically opens up a site serving any content from anywhere. And not just from the calling code. – mradcliffe Oct 25 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.