Ajax POST调用错误" application / json不是有效的输入类型"

时间:2015-06-30 10:29:40

标签: jquery ajax json google-api google-spreadsheet-api

我正在尝试执行ajax调用,但不断收到错误415 - 不支持的媒体类型。我甚至尝试设置标题Content-Type : application/json,但它仍然无效。

代码:

function addTaskInSheet(taskToUpdate){

  var accessToken = $rootScope.accessToken;
  var groupId = $rootScope.groupId;
  var url = "https://spreadsheets.google.com/feeds/list/" + groupId + "/od6/private/full" ;
  var updatedTask = {};
   $.ajax({
      url:url,
       type: 'POST',
       async: false,
       data: JSON.stringify(taskToUpdate),

       dataType: 'json',
        contentType: 'application/json',
        crossDomain: true,
        beforeSend: function (xhr) {
            xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
            xhr.setRequestHeader('Content-Type','application/json');
        },
       success: function (response) {
            updatedTask = response;
            console.log(updatedTask);

        }, error: function (response) {

          console.log(response);
       }
   });
   return updatedTask;
}

控制台出错:

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试在ajax Accept方法中添加beforeSend标题,如下:

..
beforeSend: function (xhr) {
    xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);    
    xhr.setRequestHeader('Accept','application/json');
    xhr.setRequestHeader('Content-Type','application/json');
},
..