-2

I have two sites, site A is just html and javascript, and site B has php. What I need is to get variables from site B in site A.

EX:

site A is like

<html>
<head>
  <script>
  //this script has to get the values from siteB
  </script>
</head>
<body>
  <div><!-- here i will do something with the data of site B --></div>
</body>
</html>

Site b is like:

<?php
  var1= "something";
  var2= "somethingElse";
?>

I was thinking to use JSON or Ajax but i do not understand exactly how.

leojg
  • 1,156
  • 3
  • 16
  • 40
  • Possible duplicate of http://stackoverflow.com/questions/13994858/javascript-convert-php-json-into-a-javascript-array – Henrik Peinar Jan 31 '13 at 14:26
  • Ok, thanks to all of you that had answered. Sorry if it was a duplicate, it seems that I was not able to make myself uderstand by the stack overflow search engine. I manage to do what I need with ajax. – leojg Jan 31 '13 at 14:51

3 Answers3

0
$(document).ready(function() {
  $.ajax({
   type: "GET",
   url: "filename.html",
   dataType: "json",
   success: function(data) {
        // data will contain var1 and var2
   },
   error: function(data) {
        alert("Problem - perhaps malformed JSON?");
   }
  });
});

and change your PHP file to be something like:

{
   "var1" : "something",
   "var2" : "somethingElse"
}

Confirmed to work. Make sure that your file is a well-formed JSON, otherwise "success" won't be fire.

Note - I am implying usage of JQuery here. Your HTML file should include:

<script type="txt/javascript" src="jquery-1.8b1.js"></script>
Emanuele Ciriachi
  • 2,216
  • 2
  • 26
  • 39
  • **remote** server. That will break on the same origin policy. Well, it would if the PHP didn't throw a 500 error. That is a chunk of JSON, not PHP. Oh, it breaks before it gets that far, `$` is undefined. – Quentin Jan 31 '13 at 14:32
0

File B

<?php

$array[var1] = 'Something';
$array[var2] = 'else';

echo json_encode( $array );

File A (jQuery)

$.getJSON( $( 'file.php', function( data ) {

    $( 'div' ).html( data.var1 + ' ' + data.var2 );

}

Edited -- As mentioned, can't do this cross domain without doing some other measures.

rlatief
  • 715
  • 5
  • 14
-1

Javascript cannot use ajax cross site, for security reasons. The only way to make this happen is to have but one php file on site A that can redirect.

<?php echo file_get_contents($_GET["url"]); ?>

And the javascript can call the url:

/redir.php?url=http://siteb.com/valuetoget.php

There is no way that I know of to do this with no php on the calling website.

Bob
  • 185
  • 1
  • 4
  • 13
  • There are [several ways around the same origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) but an HTTP redirect is not one of them. – Quentin Jan 31 '13 at 14:34
  • After your edit, the PHP is now performing a proxy not a redirect, it is failing to set a suitable content-type header, and it provides no security so it makes the server an ideal relay for spammers and other malicious users. – Quentin Jan 31 '13 at 14:35
  • Where is this method not valid? JS calling same site php file or a php file retrieving data from another server? – Bob Jan 31 '13 at 14:37
  • True, I guess you would need to implement some sort of security there. – Bob Jan 31 '13 at 14:38