在Google Apps脚本中使用JSONP [英] Consuming JSONP in Google Apps Script
问题描述
我想在我的Google Apps脚本中使用以下网址:
http://a0.awsstatic.com/pricing/1/emr/pricing-mapr.min。 js
I'd like to consume the following URL in my Google Apps Script:
http://a0.awsstatic.com/pricing/1/emr/pricing-mapr.min.js
使用典型的响应= UrlFetchApp.fetch(url).getContentText()
实际上并不奏效,它只是返回由 callback()
包围的JSON。有没有办法使用Google Apps Script来使用JSONP?
Using the typical response = UrlFetchApp.fetch(url).getContentText()
doesn't really work, it just returns the JSON surrounded by callback()
. Is there a way to consume JSONP with Google Apps Script?
代码:
function myFunction() {
getEmrPricingData_();
}
/*
* Get EMR Prices
*
*/
function getEmrPricingData_() {
var emr_url = "http://a0.awsstatic.com/pricing/1/emr/pricing-mapr.min.js"
var response = UrlFetchApp.fetch(emr_url).getContentText();
Logger.log(response);
}
推荐答案
您需要定义函数callback()在你的代码中,eval()你的JSONP响应来执行该函数。例如,为了在我的项目上处理JSONP响应,我创建了这个函数:
You need to define the function callback() in your code, and eval() your JSONP response to execute that function. For example to handle JSONP responses on a project of mine I created this function:
function callback(data){
return data;
}
和我的请求函数中:
and in my request function:
var result = eval(UrlFetchApp.fetch(url + uri, options).getContentText());
这篇关于在Google Apps脚本中使用JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!