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

2
  • Possible duplicate of stackoverflow.com/questions/13994858/… Commented Jan 31, 2013 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. Commented Jan 31, 2013 at 14:51

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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

4 Comments

There are several ways around the same origin policy but an HTTP redirect is not one of them.
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.
Where is this method not valid? JS calling same site php file or a php file retrieving data from another server?
True, I guess you would need to implement some sort of security there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.