javascript - 将 XMLHttpRequest 与 Google Calendar API v3 日历结合使用 :get Request

标签 javascript google-api google-calendar-api

我似乎无法让 XMLHttpRequst 方法与 Calendars:get 来自 Google Calendar API 的请求正常工作。
例如,带有“[email protected]”的calendarId可以正常工作,但是当它包含像"en.usa#<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f79f989b9e93968eb79085988287d981d994969b9299939685d9909898909b92d994989a" rel="noreferrer noopener nofollow">[email protected]</a>"这样的“#”字符时它将返回 401 错误和 "#<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="197a76776d787a6d6a597e6b766c69376f377a78757c777d786b377e76767e757c377a7674" rel="noreferrer noopener nofollow">[email protected]</a>"返回 404 错误。
我假设请求将哈希符号注册为片段,其余部分注册为片段标识符?在仍然使用 XMLHttpRequest 的同时,有什么办法可以解决这个问题吗?

var xhr = new XMLHttpRequest();
xhr.open('GET',
  'https://www.googleapis.com/calendar/v3/calendars/' + calendarId +
  '?access_token=' + accessToken);
xhr.onreadystatechange = function (e) {
  if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
    console.log(xhr.response);
  }
};
xhr.setRequestHeader('responseType', "json");
xhr.setRequestHeader('timeMin', (new Date(today.getFullYear(), 
  today.getMonth(), today.getDate())).toISOString());
xhr.setRequestHeader('showDeleted', false);
xhr.setRequestHeader('singleEvents', true);
xhr.setRequestHeader('maxResults', 30);
xhr.setRequestHeader('orderBy', "startTime");
xhr.send(null);

在 chrome 检查工具的“网络”选项卡中,与其他不带 # 的 calendarId 相比,仅发送“#”之前的请求 URL 部分。

最佳答案

I'm assuming the request is registering the hash sign as a fragment and the rest as a fragment identifiers?

是的

Is there any way around this while still using XMLHttpRequest?

与任何其他工具使用的任何其他 URL 的方式相同。

请勿在未正确转义的情况下将数据塞入 URL(或任何其他数据格式)。

'https://www.googleapis.com/calendar/v3/calendars/' + 
    encodeURIComponent(calendarId) +
   '?access_token=' + 
    encodeURIComponent(accessToken)

关于javascript - 将 XMLHttpRequest 与 Google Calendar API v3 日历结合使用 :get Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48397002/

相关文章:

javascript - JQUERY 日期选择器将自定义图像添加到特定日期框

java - 在谷歌日历 java 上设置事件 ID

python - Google 日历 V3 和命令行应用程序

javascript - Extjs/Javascript - 在函数返回之前等待 MessageBox 响应

javascript - 我应该学习 Flash/Flex/ActionScript 还是 HTML/CSS/JS ("HTML5")?

javascript - 如何选择不包含 anchor 的元素?

ruby-on-rails - 将 Google::Auth::Stores::FileTokenStore 与数据库一起使用

android - 是否有可能获得当前位置与街道、大道或区域之间的距离?

python - 仅通过电子邮件地址使用 gmail api 访问用户个人资料信息

android - 通过日历提供程序添加的事件未显示在 Android 日历应用程序中