Response.json 不是函数
Response.json is not a function
我需要你的帮助。我正在尝试学习如何执行 POST 查询以使用 fetch 在数据库中创建新对象。我用纯javascript。但是,我收到如下所示的错误:
Mistake TypeError: response.json is not a function at sendData (scrip4t.js:22) at HTMLButtonElement.onclick
我做错了什么?谢谢
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)
}
}
您需要在它们前面加上 await
前缀。点这里了解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)
}
}
您需要使用 promise 链或 async/await 如下代码:
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type' : 'application/json'
})
.then(response => response.json())
.then(data=> console.log(data));
或
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);
}
我需要你的帮助。我正在尝试学习如何执行 POST 查询以使用 fetch 在数据库中创建新对象。我用纯javascript。但是,我收到如下所示的错误:
Mistake TypeError: response.json is not a function at sendData (scrip4t.js:22) at HTMLButtonElement.onclick
我做错了什么?谢谢
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)
}
}
您需要在它们前面加上 await
前缀。点这里了解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)
}
}
您需要使用 promise 链或 async/await 如下代码:
fetch(url, {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type' : 'application/json'
})
.then(response => response.json())
.then(data=> console.log(data));
或
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);
}