本文介绍了使用Google API的Chrome扩展程序中的PUT请求未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在代码的这一点上,在该代码中,我已经使用PUT请求成功调用了Sheets API,但未在Google Sheet上呈现.

I'm stuck at this point of my code wherein I have successfully called the Sheets API using PUT request, but it's not rendering on the Google Sheet.

这是我的代码,在这里我同时使用PUT和GET请求来查看数据是否已更改:

Here is my code where I use both PUT and GET requests to see if the data changed:

background.js

background.js

chrome.identity.getAuthToken({ 'interactive': true }, getToken);

function getToken(token) {

console.log('this is the token: ', token);

  var params = {
    "range":"Sheet1!A1:B1",
    "majorDimension": "ROWS",
      "values": [
      ["Hi","Crush"]
    ],
  }
  let init = {
    method: 'PUT',
    async: true,
    data: params,
    headers: {
      Authorization: 'Bearer ' + token,
      'Content-Type': 'application/json'
    },
    'contentType': 'json',
  };
  fetch(
      "https://sheets.googleapis.com/v4/spreadsheets/1efS6aMlPFqHJJdG8tQw-BNlv9WbA21jQlufsgtMsUmw/values/Sheet1!A1:B1?valueInputOption=USER_ENTERED",
      init)
      .then((response) => console.log(response))

      let request = {
        method: 'GET',
        async: true,
        headers: {
          Authorization: 'Bearer ' + token,
          'Content-Type': 'application/json'
        },
        'contentType': 'json',
      };
      fetch(
          "https://sheets.googleapis.com/v4/spreadsheets/1efS6aMlPFqHJJdG8tQw-BNlv9WbA21jQlufsgtMsUmw/values/Sheet1!A1:B1",
          request)
          .then((response) => response.json())
          .then(function(data) {
            console.log(data)
          });
}

这是我的Google表格的屏幕截图,数据未更改. PUT请求的状态为200,看来数据仍然在A1:B1中的 Hello World :

Here's the screenshot of my Google Sheet, the data didn't change. The status of the PUT request is 200 and it seems the data is still Hello World in A1:B1:

这是日志:

您知道这里缺少什么吗?

Do you have any idea what's missing here?

推荐答案

此修改如何?请按如下所示修改init的对象.

How about this modification? Please modify the object of init as follows.

data: params,

收件人:

body: JSON.stringify(params),

参考:

  • 使用提取
  • Reference:

    • Using Fetch
    • 这篇关于使用Google API的Chrome扩展程序中的PUT请求未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:46