Javascript ReferenceError:XMLHttpRequest未在Google应用程序脚本中定义
我从一个API中得到了这个javascript,我正试图查询这个API,并将其放在GoogleApps脚本中。但是我得到了ReferenceError:XMLHttpRequest不是定义错误。我在谷歌上搜索,发现它不受支持,但我找不到一个如何绕过它的解决方案?需要帮忙吗Javascript ReferenceError:XMLHttpRequest未在Google应用程序脚本中定义,javascript,google-apps-script,Javascript,Google Apps Script,我从一个API中得到了这个javascript,我正试图查询这个API,并将其放在GoogleApps脚本中。但是我得到了ReferenceError:XMLHttpRequest不是定义错误。我在谷歌上搜索,发现它不受支持,但我找不到一个如何绕过它的解决方案?需要帮忙吗 function test (){ var request = new XMLHttpRequest(); request.open('POST', 'https://api...'); request.setReque
function test (){
var request = new XMLHttpRequest();
request.open('POST', 'https://api...');
request.setRequestHeader('Content-Type', 'application/json');
request.onreadystatechange = function () {
if (this.readyState === 4) {
console.log('Status:', this.status);
console.log('Headers:', this.getAllResponseHeaders());
console.log('Body:', this.responseText);
}
};
var body = {
'channels': [
'11111111',
'22222222'
],
'metrics': [
'post_video_views_10s_organic',
'post_consumptions_unique'
],
'since': '2017-01-01',
'until': '2017-03-02',
'postsSince': '2016-12-24',
'postsUntil': '2016-12-31',
'orderBy': 'metrics.post_impressions',
'direction': 'ASC',
'limit': 10,
'offset': 0
};
request.send(JSON.stringify(body));} ```
改用UrlFetchApp。
以下是来自以下站点的示例:
// Make a POST request with a JSON payload.
var data = {
'name': 'Bob Smith',
'age': 35,
'pets': ['fido', 'fluffy']
};
var options = {
'method' : 'post',
'contentType': 'application/json',
// Convert the JavaScript object to a JSON string.
'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch('https://httpbin.org/post', options);
有关文档,请参见此处: