doupao6698 2013-01-31 14:24
浏览 31
已采纳

在javascript中从远程php服务器获取价值

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.

  • 写回答

3条回答 默认 最新

  • dongxing8766 2013-01-31 14:29
    关注
    $(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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样