Google Apps脚本在服务器端相当于$ .ajax()的作用是什么? [英] What's the server-side equivalent of $.ajax() in Google Apps Scripts?

查看:120
本文介绍了Google Apps脚本在服务器端相当于$ .ajax()的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从带有Authorization标头的Google App脚本中的服务器端代码执行HTTP请求.是否有用于发送HTTP请求的App Script API?

I want to perform an HTTP request from server-side code in a Google App Script with an Authorization header. Is there an App Script API for sending HTTP requests?

Google Apps脚本中的这段代码等效于什么?

What's the equivalent of this code in a Google Apps Script?

 var api = "URL";
 $.ajax({
     type: 'GET',
     url: api,
     contentType: 'application/json',
     dataType:'json',
     data: {},
     beforeSend: function(xhr) {
         xhr.setRequestHeader('Authorization', makeBaseAuth('username', 'password'));
     }
});

推荐答案

您可以使用 HTTPResponse 包含有关HTTP提取结果的信息.

You can send HTTP requests using the UrlFetchApp object. It has a fetch(url, params) method that returns a HTTPResponse with information about the result of the HTTP fetch.

function testapi(){

    var encode =  Utilities.base64Encode('username:password', Utilities.Charset.UTF_8);
    Logger.log(encode);

    var option = {
      headers : {
            Authorization: "Basic "+ encode
      }   
    }

    var url = "URL";
    var response = UrlFetchApp.fetch(url, option).getContentText()
    response = JSON.parse(response);

    for (var key in response){
      Logger.log(key);
    }
}

这篇关于Google Apps脚本在服务器端相当于$ .ajax()的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆