简体   繁体   English

将XMLHttpRequest与Google Calendar API v3 Calendars结合使用:获取请求

[英]Using XMLHttpRequest with Google Calendar API v3 Calendars:get Request

I can't seem to get the XMLHttpRequst method to work properly with the Calendars:get request from the Google Calendar API. 我似乎无法让XMLHttpRequst方法与Google Calendar API的Calendars:get请求一起正常使用。
For example, a calendarId with "myemail@gmail.com" works properly, but When it includes a "#" character like "en.usa#holiday@group.v.calendar.google.com" it will return a 401 error and "#contacts@group.v.calendar.google.com" returns a 404 error. 例如,带有“ myemail@gmail.com”的calendarId可以正常工作,但是当它包含“#”字符(例如"en.usa#holiday@group.v.calendar.google.com" ,它将返回401错误, "#contacts@group.v.calendar.google.com"返回404错误。
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? 在仍然使用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);

In the Network tab of the chrome inspect tool, only the part of the request url before the "#" appears to be sent in comparison to the other calendarId's without #'s. 在chrome检查工具的“网络”标签中,与其他不含#的calendarId相比,仅发送了“#”之前的请求网址部分。

I'm assuming the request is registering the hash sign as a fragment and the rest as a fragment identifiers? 我假设请求将哈希符号注册为一个片段,其余注册为片段标识符?

Yes

Is there any way around this while still using XMLHttpRequest? 在仍然使用XMLHttpRequest的情况下,有什么解决办法?

The same way as for any other URL used by any other tool. 与任何其他工具使用的任何其他URL相同。

Don't shove data into URLs (or any other data format) without properly escaping it. 未经适当转义,请勿将数据推送到URL(或任何其他数据格式)中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM