AJAX使用sabreDAV调用baikal共享日历

时间:2017-10-21 12:29:27

标签: jquery ajax icalendar webdav

我想弄清楚如何对baikal服务器进行ajax调用来执行GET,POST,OPTIONS,PROPFIND,PROPPATCH,REPORT,PUT,MOVE,DELETE,LOCK,UNLOCK

我已使用此代码here并使其正常工作,但我想我只是不知道如何进行正确的ajax调用。

我在同一个域内进行调用,以避免现在出现跨域问题。我之前使用过CalDavZAP,我可以看到他们使用ajax进行调用,但即使在查看代码之后我也无法使用它。

我试过了

var content = 'BEGIN:VCALENDAR VERSION:2.0 CALSCALE:GREGORIAN BEGIN:VEVENT CREATED:20171020T123121Z LAST-MODIFIED:20171020T220236Z DTSTAMP:20171020T220236Z UID:m54url1l-6xwy-snnp-rg0h-7yl5kgsfbadg SUMMARY:this is new TRANSP:OPAQUE CLASS:PUBLIC DTSTART;VALUE=DATE:20171025 DTEND;VALUE=DATE:20171026 END:VEVENT PRODID:-//Inf-IT//CalDavZAP 0.13.1//EN END:VCALENDAR';

$.ajax({
  type: 'PUT',
  url: 'http://cal.domain.ca/cal.php/calendars/user/default/crazychars.ics',
  beforeSend: function(req) {
    req.setRequestHeader('Prefer', 'return=representation');
    req.setRequestHeader('If-None-Match', '*');
  },
  username:'username',
  password:'password',
  contentType: 'text/calendar',
  processData: true,
  data: content,
  dataType: 'text', 
  success: function(data){
    console.log(data);
  },
  error: function(){
  console.log(data);  
    event.preventDefault();
  }
});//end of AJAX

在尝试了很多事情后,上面现在返回415(不支持的媒体类型)。我尝试了不同类型的contentType和其他标题,没有更好的结果。在搜索互联网后,我不知道还能做什么。贝加尔湖网站没有提供从我看过的地方提交到服务器的示例。

这是一些请求/响应标头

Response Headers
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-
check=0
Connection:Keep-Alive
Content-Type:application/xml; charset=utf-8
Date:Sat, 21 Oct 2017 11:44:12 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=98
Pragma:no-cache
Server:Apache/2.4.6
Transfer-Encoding:chunked
X-Powered-By:PHP/5.6.31
X-Sabre-Version:3.1.3

Request Headers
Accept:text/plain, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en,fr;q=0.8
Authorization:Basic Z3N0bG91aXM6R3N0bG91aXM3OA==
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:345
Content-Type:text/calendar
Cookie:PHPSESSID=fqc0ctuskfv0cgaqeiq196gtf4; _ga=GA1.2.519255846.1496415686
Host:cal.smbservices.ca
If-None-Match:*
Origin:http://cal.domain.ca
Prefer:return=representation
Referer:http://cal.domain.ca/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
X-Requested-With:XMLHttpRequest

1 个答案:

答案 0 :(得分:1)

我遇到的问题是我的内容变量。为了使用\ r \ n

完成每行所需的格式良好的ajax调用
var content = 'BEGIN:VCALENDAR\r\nVERSION:2.0\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nCREATED:20171020T123121Z\r\nLAST-MODIFIED:20171020T220236Z\r\nDTSTAMP:20171020T220236Z\r\nUID:m54url1l-6xwy-snnp-rg0h-7yl5kgsfbadg\r\nSUMMARY:superman\r\nTRANSP:OPAQUE\r\nCLASS:PUBLIC\r\nDTSTART;VALUE=DATE:20171030\r\nDTEND;VALUE=DATE:20171030\r\nEND:VEVENT\r\nPRODID:-//Inf-IT//CalDavZAP0.13.1//EN\r\nEND:VCALENDAR';