2013-01-31 14:24
浏览 31


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.


site A is like

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

Site b is like:

  var1= "something";
  var2= "somethingElse";

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongxing8766 2013-01-31 14:29
    $(document).ready(function() {
       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>
    解决 无用
    打赏 举报
  • douduxia1551 2013-01-31 14:33

    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:


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

    解决 无用
    打赏 举报
  • dqo58772 2013-01-31 14:33

    File B

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

    解决 无用
    打赏 举报

相关推荐 更多相似问题