2012-08-15 70 views
1

仍試圖找出在Google IO 2012上啓動的所有新內容。關於GAS和ContentService,假設您可以創建API爲您的內容提供服務(來自Google雲端硬盤)。一個簡單的例子是這樣的:Google Apps腳本(GAS) - 創建ContentService API以提供來自Google Drive的JSON數據

var jsonData = { 
    "firstName": "John", 
    "lastName": "Smith", 
    "age": 25, 
    "address": { 
     "streetAddress": "21 2nd Street", 
     "city": "New York", 
     "state": "NY", 
     "postalCode": "10021" 
    }, 
    "phoneNumber": [ 
     { 
      "type": "home", 
      "number": "212 555-1234" 
     }, 
     { 
      "type": "fax", 
      "number": "646 555-4567" 
     } 
    ] 
} 

function doGet(e) { 

    var output = ContentService.createTextOutput(); 
    output.setMimeType(ContentService.MimeType.JSON); 
    output.setContent(jsonData); 

    return output; 

} 

然後從正常的網頁,希望使用jQuery阿賈克斯,你應該能夠得到JSON數據?

簡單的JavaScript代碼在普通(非GAS)客戶端上的外觀是什麼?

回答

3

看看JSONP看到它應該是什麼樣子

而且,它應該是output.setContent(JSON.stringify(jsonData));

+0

感謝科裏,即使有變化,我的超級基本樣本仍然沒有工作。你介意在GAS和Client JS上發佈一個簡單的示例嗎?目前我客戶端JS是: $阿賈克斯({ 網址:網址, 跨域:真實, 數據類型: 'JSONP', 緩存:假的, 類型: 'GET', 成功:功能(數據){ console.log(data); } }) – user1541413 2012-08-15 14:32:46

+0

不幸的是,我不能真正幫助你與客戶端。應用程序腳本部分可以通過在瀏覽器中查看該URL的結果來輕鬆進行測試。 – 2012-08-15 16:26:19