2014-11-25 61 views
1

我有一個TIdHTTPServer,它將HTML代碼中的javascript代碼發送到瀏覽器,代碼中的代碼爲<script language="javascript">。我無法訪問html文件,因此我無法更改它的內容。我用HTTPServerCommandGet這個文件的內容發送到瀏覽器這樣的:Indy TIdHTTPServer OnCommand在html中執行javascript未執行

procedure TMainForm.HTTPServerCommandGet(AContext: TIdContext; 
    ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 
var 
    responseMemoryStream : TMemoryStream; 
begin 
    responseMemoryStream := TMemoryStream.Create; 
    try 
    AResponseInfo.ContentType := 'text/html'; 
    AResponseInfo.ContentEncoding := 'UTF-8'; 
    AResponseInfo.CharSet := 'UTF-8'; 
    responseMemoryStream.LoadFromFile(ExtractFilePath(Application.ExeName) + 'index.html'); 
    AResponseInfo.ContentStream := TMemoryStream.Create; 
    TMemoryStream(AResponseInfo.ContentStream).LoadFromStream(responseMemoryStream); 
    finally 
    responseMemoryStream.Free; 
    end; 
end; 

當我然後再到有時加載JavaScript的互聯網瀏覽器GET命令,但有時沒有 - 當我使用Firefox沒有問題,但是當我使用ChromeIE JavaScript從不執行。有沒有辦法用AResponseInfo變量以某種方式強制執行javascript代碼,或者我沒有辦法解決這個問題?

這就是index.html的部分看起來像:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta name="keywords" content="" /> 
    <meta name="description" content="" /> 
    <meta charset=utf8 /> 
    <title>My title</title> 
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script language="javascript"> 
     var map; 
     var flag_for_map_massage = 0; 
     var map_marker = 0; 
     function initmap() 
     { 
      var html; 
      var map_divtxt = "<div id=\"map_canvas\" style=\"width:200px;height:150px;\" ></div>"; 
      document.getElementById("google_map").innerHTML = map_divtxt; 
      var latlng = new google.maps.LatLng(100.12345, 200.12345); 

      var options = { 
       zoom: 17, 
       center: latlng, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      var map1 = new google.maps.Map(document.getElementById("map_canvas"), options); 

      var html = "<table>" + 
        "<tr><td>GMBH Address</td> </tr>"; 

      infowindow = new google.maps.InfoWindow({ 
       content: html 
      }); 
      var marker1 = new google.maps.Marker({ 
       position: new google.maps.LatLng(100.12345, 200.12345), 
       map: map1 

      }); 
      //  infowindow.open(map1, marker1); 
      google.maps.event.addListener(marker1, "click", function() { 
       infowindow.open(map1, marker1); 
      }); 
     } 

    </script> 
</head> 
<body onload="initmap();"> 
    <div class="entry"> 
    <div id="google_map"> 
    </div> 
    </div> 
</body> 
</html> 
+0

我剛剛看到這段代碼'var latlng = new google.maps.LatLng(100.12345,200。12345);當''OnCommandGet''發送時''不能被執行(後面的所有內容),但如果我用瀏覽器打開文件本身 - 它會執行正常。 – 2014-11-25 13:56:36

+0

@whosrdaddy - 無處 - 顯然它們會以某種方式自動加載(當我在本地加載文件後檢查瀏覽器中的元素時,我會在「」標記中看到3個Google Maps API''',並且它只能在Firefox中使用 - IE和Chrome無法加載地圖。 – 2014-11-25 14:26:55

回答

4

所有服務器所能做的是提供的HTML瀏覽器,僅此而已。瀏覽器負責處理HTML,檢索外部資源,執行腳本等。如果瀏覽器運行不正常,那麼HTML /腳本可能會出現錯誤。這與你的服務器無關(除非你正在破壞你發送的數據)。

這樣說,你不需要在你的OnCommandGet代碼中使用兩個流,這是矯枉過正。一個流就夠了。另外,utf-8不是有效的ContentEncoding值,因此您需要刪除該值。

試試這個:

procedure TMainForm.HTTPServerCommandGet(AContext: TIdContext; 
    ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 
begin 
    AResponseInfo.ContentType := 'text/html'; 
    AResponseInfo.CharSet := 'utf-8'; 
    AResponseInfo.ContentStream := TMemoryStream.Create; 
    TMemoryStream(AResponseInfo.ContentStream).LoadFromFile(ExtractFilePath(Application.ExeName) + 'index.html'); 
end; 

或者:

procedure TMainForm.HTTPServerCommandGet(AContext: TIdContext; 
    ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo); 
begin 
    AResponseInfo.ContentType := 'text/html'; 
    AResponseInfo.CharSet := 'utf-8'; 
    AResponseInfo.ContentStream := TIdReadFileExclusiveStream.Create(ExtractFilePath(Application.ExeName) + 'index.html'); 
end; 

,當然,請確保您正在關注的ARequestInfo.Document財產,只提供index.html如果瀏覽器實際上是要求index.html和而不是服務器上的其他文件。

2

Indy10的IdCustomHTTPServer.pas有一個錯誤。

設置AResponseInfo.Charset值不起作用。 因爲以下行(Pas文件中的第2141行)會覆蓋Charset變量。

if ContentType = '' then begin 
    if (ContentText <> '') or (Assigned(ContentStream)) then begin 
     ContentType := 'text/html; charset=ISO-8859-1'; {Do not Localize} 

爲了糾正,下面(複製從添加兩個文件, 「C:\ Program Files文件(x86)的\ Embarcadero公司\工作室\ 16.0 \源\ Indy10 \核心」 到你的項目目錄,然後添加到您的項目):

IdCustomHTTP.pas

IdCompilerDefines.inc

修改線2141作爲

ContentType := 'text/html; charset='+Charset; 

;-)