Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

316
Visualizações
Invalid JSON Payload Received writing to Google Sheet from JavaScript/Ajax

My goal is to write to an existing Google Sheet via a Ajax/JavaScript call on the client-side.

However, I'm currently getting the following error:

Invalid JSON payload received. Unknown name "{"range":"Sheet1!A8:C8","majorDimension":"ROWS","values":[["Field1","Field2","Field3"]]}": Cannot bind query parameter. Field '{"range":"Sheet1!A8:C8","majorDimension":"ROWS","values":[["Field1","Field2","Field3"]]}' could not be found in request message.

This is my JavaScript code:

const sheetID = "--privateField--";
const key = "--privateField--";
var url = "https://sheets.googleapis.com/v4/spreadsheets/"+sheetID+"/values:batchUpdate/?key="+key;
$.ajax({
    url: url,
    type: 'PUT',
    dataType: 'jsonp',
    valueInputOption: 'USER_ENTERED',
    data: JSON.stringify({
        range: 'Sheet1!A8:C8',
        majorDimension: 'ROWS',
        values: [
            ['Field1', 'Field2', 'Field3']
        ]
    }),
    headers: {
        "Content-Type": "application/json"
    },
    success: function(data) {
        console.log(data);
    },
    error: function(data) {
        console.log(data);
    }

Where sheetID & key are replaced with actual production values.

I've tried many different variations of this API call based on different Stack Overflow results I've found, but haven't been able to get anything working. It seems to me like I've specified 'Values' correctly (an array of arrays), but the error message seems to indicate otherwise.

I've been able to read from Google Sheets using Ajax/JavaScript on the client-side before, and am now trying to get an example working for writing to the sheet.

Any advice or tips would be greatly appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Mod Points:

  • Since My goal is to write to an existing Google Sheet via a Ajax/JavaScript call on the client-side. and your script showing, in this case the API key cannot be used to update the spreadsheet. Unfortunately, it appears that this is the current specification. In this case, it is required to use the access token. Be careful with this.

    • To retrieve the access token, I thought this official document is helpful. Ref And, I think you can also see it on Stackoverflow.

  • When you want to use the Sheets API spreadsheets.values.batchUpdate method, in your script, use of the POST method is required.

  • And it is required to modify the body of the request.

I thought that the reason for your current problem is due to the above points. When these points are reflected in your script, it becomes the following.

Modified script:

 const sheetID = "--privateField--"; const accessToken = "### your access token ###"; var url = "https://sheets.googleapis.com/v4/spreadsheets/" + sheetID + "/values:batchUpdate"; var requestBody = { valueInputOption: 'USER_ENTERED', data: [{ range: 'Sheet1!A8:C8', majorDimension: 'ROWS', values: [['Field1', 'Field2', 'Field3']] }] }; $.ajax({ url: url, type: 'POST', data: JSON.stringify(requestBody), headers: { "Content-Type": "application/json", "Authorization": "Bearer " + accessToken }, success: function (data) { console.log(data); }, error: function (data) { console.log(data); } });
  • When I tested this script, I confirmed that the values of [['Field1', 'Field2', 'Field3']] are placed in row 8 in "Sheet1".

References:

  • Method: spreadsheets.values.batchUpdate

  • Using OAuth 2.0 to access Google APIs

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda