2016-07-24 152 views
0

我有一個需要令牌訪問某些數據的HTML文件(來自ArcGIS Online)。一個單獨的JavaScript文件應該調用服務並獲取令牌。令牌然後需要以某種方式傳遞到HTML文件,這是我不確定的位。如何將JavaScript響應轉換爲HTML

在任何情況下,代碼:

JavaScript文件(GetAToken.js)

var request = require('request'); // npm install request 

// generate a token with your client id and client secret 
function getToken(callback) 
{ 
    request.post(
    { 
     url: 'https://www.arcgis.com/sharing/rest/oauth2/token/', 
     json: true, 
     form: 
     { 
      'f': 'json', 
      'client_id': '<<MY CLIENT_ID>>', 
      'client_secret': '<<MY CLIENT_SECRET>>', 
      'grant_type': 'client_credentials', 
      'expiration': '1440' 
     } 
    }, function (error, response, body) 
    { 
     console.log(body.access_token); 
     callback(body.access_token); 
    }); 
} 

而從HTML中的相關位

<script src="GetAToken.js"></script> 
</head> 
<body onload="getToken()"> 
<div class="embed-container"> 
    <iframe width="500" 
      height="400" 
      frameborder="0" 
      scrolling="no" 
      marginheight="0" 
      marginwidth="0" 
      title="Test Map" src="//MyMap.maps.arcgis.com/apps/webappviewer/index.html?id=LongMapID?token=I_Need_My_Token_Here&amp;extent=1,-1,1,-1&amp;zoom=true&amp;scale=true&amp;search=true&amp;searchextent=true&amp;legend=true&amp;basemap_gallery=true&amp;disable_scroll=true&amp;theme=light"> 
    </iframe> 
</div> 

</body> 
</html> 

如果你看看內HTML中的div,這就是我需要令牌去的地方。 JavaScript的顯然是返回一個名爲access_token值,並寫入使用Node.js的

編輯

新GetAToken.js

const request = require('request'); // npm install request 
const express = require('express'); 
const app = express(); 

// generate a token with your client id and client secret 
//function getToken(callback) 
app.get('/GetAToken', (req, res) => { 
    request.post(
    { 
     url: 'https://www.arcgis.com/sharing/rest/oauth2/token/', 
     json: true, 
     form: 
     { 
      'f': 'json', 
      'client_id': '<<MY_CLIENT_ID>>', 
      'client_secret': '<<MY_CLIENT_SECRET>>', 
      'grant_type': 'client_credentials', 
      'expiration': '1440' 
     } 
    }, function (error, response, body) { 
     console.log(body.access_token); 
     callback(body.access_token); 
    }); 
}); 
app.listen(80); 

更新HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> 
    <title>Title</title> 
    <link href="https://esri.github.io/calcite-bootstrap/assets/css/calcite-bootstrap-open.min.css" rel="stylesheet"> 
    <style> 
     .footer 
     { 
      height: 6.25rem; 
     } 
    </style> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> 
    <script src="https://esri.github.io/calcite-bootstrap/assets/js/bootstrap.min.js"></script> 
    <script src="https://js.arcgis.com/3.17/"></script> 
    <script src="GetAToken.js"></script> 
    <script type="text/javascript"> 

     var xhttp = new XMLHttpRequest(); 
     xhttp.onreadystatechange = function() 
     { 
      if(xhttp.readyState === 4 && xhttp.status === 200) 
      { 
       var responseJSON = JSON.parse(xhttp.responseText); 
       var token = responseJSON.token; 
       alert(token); 
      } 
     } 
     xhttp.open("GET", "GetAToken", true); 
     xhttp.send();  

    </script> 

</head> 
<body> 

    <style> 
     .embed-container 
     { 
      position: relative; 
      padding-bottom: 50%; 
      height: 0; 
      max-width: 100%; 
     } 
     .embed-container iframe, .embed-container object, .embed-container iframe 
     { 
      position: absolute; 
      top: 0; 
      left: 0; 
      width: 100%; 
      height: 100%; 
     } 
     small 
     { 
      position: absolute; 
      z-index: 40; 
      bottom: 0; 
      margin-bottom: -15px; 
     } 
    </style> 
    <div class="embed-container"> 
     <iframe width="500" 
       height="400" 
       frameborder="0" 
       scrolling="no" 
       marginheight="0" 
       marginwidth="0" 
       title="Test Map" src="//MyMap.maps.arcgis.com/apps/webappviewer/index.html?id=MyLongID&amp;extent=1,-1,1,-1&amp;zoom=true&amp;scale=true&amp;search=true&amp;searchextent=true&amp;legend=true&amp;basemap_gallery=true&amp;disable_scroll=true&amp;theme=light"> 
     </iframe> 
    </div> 

</body> 
</html> 
+0

是'GetAToken'服務器或客戶端上運行之前解決這個問題? –

+0

我不確定。我對JavaScript非常陌生。它與我的HTML目前位於同一個文件夾中,我最終希望它能夠在互聯網上運行。 – user25730

+0

它應該在服務器上運行,因爲它具有密鑰。 – alexi2

回答

1

你想要將該請求的響應以某種方式提供給客戶端。下面是一個例子使用快遞:

const request = require('request'); // npm install request 
const express = require('express'); // npm install express 
const app = express(); 

app.get('/get-a-token', (req, res) => 
{ 
     request.post(
     { 
       url: 'https://www.arcgis.com/sharing/rest/oauth2/token/', 
       json: true, 
       form: 
       { 
         'f': 'json', 
         'client_id': '<<MY CLIENT_ID>>', 
         'client_secret': '<<MY CLIENT_SECRET>>', 
         'grant_type': 'client_credentials', 
         'expiration': '1440' 
       } 
     }, function (error, response, body) 
     { 
       console.log(body.access_token); 
       res.json({token: body.access_token}); 
     }); 
}); 

app.listen(80); 

然後在客戶端上,你可以做這樣的事情來從服務器獲取的值:

<script type="text/javascript"> 
    // You may want to move this to another file.. 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
     if (xhttp.readyState === 4 && xhttp.status === 200) { 
      var responseJSON = JSON.parse(xhttp.responseText); 
      var token = responseJSON.token; 

      var iframe = document.querySelectorAll('iframe')[0] 
      iframe.src = "//MyMap.maps.arcgix.com/apps/webappviewer/index.html?id=LongMapID?token=" + token + "&amp;extent=1,-1,1,-1&amp;zoom=true&amp;scale=true&amp;search=true&amp;searchextent=true&amp;legend=true&amp;basemap_gallery=true&amp;disable_scroll=true&amp;theme=light" 
     } 
    } 
    xhttp.open("GET", "http://yournodeserver.com/get-a-token", true); 
    xhttp.send(); 
</script> 

你可能會想做些什麼來保護/get-a-token路線從您的網站以外訪問。

如果您服務與節點HTML文件/快遞太多,那麼你可以通過將令牌的HTML它的服務給客戶,而不是

+0

不幸的是,console.log在init.js:19中返回'Uncaught Error:undefinedModule',並且在HTML中還有'Uncaught ReferenceError:send is not defined'。 – user25730

+1

你可以發佈你使用的代碼嗎? console.log不會記錄一個錯誤 - 只有access_token的值,但可能有其他代碼會拋出它。 – carlevans719

+0

我基本上使用上面的代碼。但是,我不確定我是否正確安裝了node.js。 – user25730