Javascript 获取API请求后响应
我试图在提交表单后从post请求中获得输出,但在发布表单时,我得到的是承诺响应,而不是实际数据Javascript 获取API请求后响应,javascript,react-native,fetch,Javascript,React Native,Fetch,我试图在提交表单后从post请求中获得输出,但在发布表单时,我得到的是承诺响应,而不是实际数据 fetch('localhost/clients', { method: 'post', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(formData) }).then(response => { con
fetch('localhost/clients', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(formData)
}).then(response => {
console.info('Sending Message ...')
console.info(response.json())
}).catch (error => {
console.log(error)
})
数据被传递到后端,但是我想返回API服务器输出的数据。response.json返回承诺。你需要像下面那样使用它
fetch('localhost/clients', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(formData)
}).then(response => {
return response.json();
}).then(jsonResponse => {
console.log(jsonResponse);
}).catch (error => {
console.log(error)
})