0

我正在尝试做以下事情:我想从位于 Web 服务器中的 page.html 获取两个动态数据(纬度和经度)我想将它们作为 Web 服务获取:这是执行的结果我的网页位于服务器中:{“results”:[{“Latitude”:“36.81881”,“Longitude”:“10.16596”}]} 这正是我想在我的客户端应用程序中恢复的,但问题是我无法得到结果,我只能得到页面的代码源。Html 位于服务器中。这是我在客户端用于从服务器获取数据的方法:

function latLong()
{

    var ajax = new XMLHttpRequest();
ajax.open("GET","http://localhost/AtitLongiWebService.html",true);
ajax.send();
ajax.onreadystatechange=function(){
     if(ajax.readyState==4 && (ajax.status==200||ajax.status==0)){
     alert(ajax.responseText);    
 }
     }
}

这是我在网络服务器中的 page.html:

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <script type="text/javascript">

    function startWatch(){
      if (navigator.geolocation)
        var watchId = navigator.geolocation.watchPosition(successCallback,
                                  errorCallback,
                                  {enableHighAccuracy:true,
                                  timeout:10000,
                                  maximumAge:0});
      else
        alert("Votre navigateur ne prend pas en compte la géolocalisation HTML5");
    }

    function stopWatch(){
      navigator.geolocation.clearWatch(watchId);
    }     

    function successCallback(position){
      document.getElementById("lat").innerHTML = position.coords.latitude;
      document.getElementById("long").innerHTML = position.coords.longitude;
    };  

    function errorCallback(error){
      switch(error.code){
        case error.PERMISSION_DENIED:
          alert("L'utilisateur n'a pas autorisé l'accès à sa position");
          break;      
        case error.POSITION_UNAVAILABLE:
          alert("L'emplacement de l'utilisateur n'a pas pu être déterminé");
          break;
        case error.TIMEOUT:
          alert("Le service n'a pas répondu à temps");
          break;
        }
    };

  </script>
    </head>
    <body onload="startWatch()">

        {"results":[{"Latitude":"<label id="lat"></label>",
        "Longitude": "<label id="long"></label>"}]}


    </body>
</html>

通常 ajax.responseText 返回结果,但在这种情况下它返回代码源,有人可以帮我获取页面执行的结果,而不是代码源。谢谢您的帮助。

4

1 回答 1

0

目前您正在访问的网页是一个标准的静态 HTML 页面,其中嵌入了 javascript。由于这是由 Apache 提供的(作为 WAMPserver 环境的一部分),因此它将其视为带有客户端javascript 的纯 HTML 页面。也就是说,只需将文件发送到客户端而不运行任何内容。

为了运行服务器端javascript,您需要有一个javascript 服务器,例如node.js

于 2013-04-15T20:25:23.707 回答