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

219
Visualizações
Response.json is not a function

I need your help. I'm trying to learn how to execute a POST query to create a new object in a database using fetch. I use pure javascript. However, I get an error that looks like this:

Mistake TypeError: response.json is not a function at sendData (scrip4t.js:22) at HTMLButtonElement.onclick

What am I doing wrong? Thanks

HTML

<input type="text" id="name">
<input type="text" id="price">
<input type="text" id="description">
<button id="send" onclick="sendData()">submit</button>

JS

const url = `http://localhost:8081/laptop`
let name = document.getElementById('name')
let price = document.getElementById('price')
let description = document.getElementById('description')

function sendData () {

let name_of_laptop = name.value
let price_of_laptop = price.value
let laptop_description = description.value

let data = {name: name_of_laptop, price: price_of_laptop, description: laptop_description}

try {
    const response = fetch(url, {
        method: 'POST',
        body: JSON.stringify(data),
        headers: {
            'Content-Type' : 'application/json'
        }
    });
    let json = response.json();
    console.log('Success', JSON.stringify(json))
} catch (error) {
    console.error('Mistake',error)
 }
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to prefix them with await. Check here to understand async/await

...

async function sendData () {

...

try {
    const response = await fetch(url, {
        method: 'POST',
        body: JSON.stringify(data),
        headers: {
            'Content-Type' : 'application/json'
        }
    });

    let json = await response.json();
    console.log('Success', JSON.stringify(json))
} catch (error) {
    console.error('Mistake',error)
 }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to use promise chain or async/await like below code:

fetch(url, {
    method: 'POST',
    body: JSON.stringify(data),
    headers: {
        'Content-Type' : 'application/json'
    })
.then(response => response.json())
.then(data=> console.log(data));

or

async function sendData () 
 const response = await fetch(url, {
    method: 'POST',
    body: JSON.stringify(data),
    headers: {
        'Content-Type' : 'application/json'
    }
 });

 let data = await response.json();
 console.log(data);
}
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