Javascript 我如何使用Fetch发布x-www-form-urlencoded请求并使用答案?
这是我的代码:Javascript 我如何使用Fetch发布x-www-form-urlencoded请求并使用答案?,javascript,fetch,x-www-form-urlencoded,Javascript,Fetch,X Www Form Urlencoded,这是我的代码: fetch('http://localhost:3000', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ 'size': 'size_id', 'style': 'style_id', 'qty': '1' }) }) .then(res =>
fetch('http://localhost:3000', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'size': 'size_id',
'style': 'style_id',
'qty': '1'
})
})
.then(res => {
console.log(res)
});
我的问题是,我只是得到了'承诺待定'返回。
我对fetch完全陌生,对js也很陌生,所以请不要怪我。res
是响应数据,而不是响应数据
你在等json吗?然后做:
fetch('http://localhost:3000', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'size': 'size_id',
'style': 'style_id',
'qty': '1'
})
})
.then(res => res.json())
.then(res => {
console.log(res)
});
否则,获取如下所示的普通响应数据/文本:
fetch('http://localhost:3000', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
'size': 'size_id',
'style': 'style_id',
'qty': '1'
})
})
.then(res => res.text())
.then(res => {
console.log(res)
});
我会试试的,谢谢!