Consuming JSONP in Google Apps Script

安稳与你 提交于 2019-12-25 04:46:20

问题


I'd like to consume the following URL in my Google Apps Script: http://a0.awsstatic.com/pricing/1/emr/pricing-mapr.min.js

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?

Code:

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);
}

回答1:


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());


来源:https://stackoverflow.com/questions/28092281/consuming-jsonp-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!