Javascript 如何显示从服务器返回的json对象
当HTTP状态不是200时,如何获取后端在前端代码中发送的JSON对象 当我的后端发送200响应时,我可以通过以下操作显示返回的JSON数据: 后端Javascript 如何显示从服务器返回的json对象,javascript,json,frontend,axios,backend,Javascript,Json,Frontend,Axios,Backend,当HTTP状态不是200时,如何获取后端在前端代码中发送的JSON对象 当我的后端发送200响应时,我可以通过以下操作显示返回的JSON数据: 后端 res.status(200).json({status: 'Information found'}) .then(data => console.log(data.status)) // Information found res.status(404).json({error: 'No information found'}); .
res.status(200).json({status: 'Information found'})
.then(data => console.log(data.status)) // Information found
res.status(404).json({error: 'No information found'});
.catch(error => console.log('Error from backend', error))
axios.post('https://firebaselink.net/getTestData', { id: orderNumber })
.then(data => console.log(data.status))
.catch(error => console.error(error))
export const getTestData = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const id = req.body.id;
admin.database().ref(`data/${id}`).once('value')
.then(snapshot => {
if(snapshot) res.status(200).json({status: "Information found"})
else res.status(404).json({error: "Information not found"})
}).catch(error => res.status(500).json({error: 'Internal error}))
})
})
前端
res.status(200).json({status: 'Information found'})
.then(data => console.log(data.status)) // Information found
res.status(404).json({error: 'No information found'});
.catch(error => console.log('Error from backend', error))
axios.post('https://firebaselink.net/getTestData', { id: orderNumber })
.then(data => console.log(data.status))
.catch(error => console.error(error))
export const getTestData = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const id = req.body.id;
admin.database().ref(`data/${id}`).once('value')
.then(snapshot => {
if(snapshot) res.status(200).json({status: "Information found"})
else res.status(404).json({error: "Information not found"})
}).catch(error => res.status(500).json({error: 'Internal error}))
})
})
但是,当我尝试对400或404状态执行相同操作时,我无法获取返回的JSON
后端
res.status(200).json({status: 'Information found'})
.then(data => console.log(data.status)) // Information found
res.status(404).json({error: 'No information found'});
.catch(error => console.log('Error from backend', error))
axios.post('https://firebaselink.net/getTestData', { id: orderNumber })
.then(data => console.log(data.status))
.catch(error => console.error(error))
export const getTestData = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const id = req.body.id;
admin.database().ref(`data/${id}`).once('value')
.then(snapshot => {
if(snapshot) res.status(200).json({status: "Information found"})
else res.status(404).json({error: "Information not found"})
}).catch(error => res.status(500).json({error: 'Internal error}))
})
})
前端
res.status(200).json({status: 'Information found'})
.then(data => console.log(data.status)) // Information found
res.status(404).json({error: 'No information found'});
.catch(error => console.log('Error from backend', error))
axios.post('https://firebaselink.net/getTestData', { id: orderNumber })
.then(data => console.log(data.status))
.catch(error => console.error(error))
export const getTestData = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const id = req.body.id;
admin.database().ref(`data/${id}`).once('value')
.then(snapshot => {
if(snapshot) res.status(200).json({status: "Information found"})
else res.status(404).json({error: "Information not found"})
}).catch(error => res.status(500).json({error: 'Internal error}))
})
})
结果
我第一次尝试了error.error,但没有定义
200状态返回有什么区别?我如何从前端的后端获取发送的消息
编辑
前端
res.status(200).json({status: 'Information found'})
.then(data => console.log(data.status)) // Information found
res.status(404).json({error: 'No information found'});
.catch(error => console.log('Error from backend', error))
axios.post('https://firebaselink.net/getTestData', { id: orderNumber })
.then(data => console.log(data.status))
.catch(error => console.error(error))
export const getTestData = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const id = req.body.id;
admin.database().ref(`data/${id}`).once('value')
.then(snapshot => {
if(snapshot) res.status(200).json({status: "Information found"})
else res.status(404).json({error: "Information not found"})
}).catch(error => res.status(500).json({error: 'Internal error}))
})
})
后端
res.status(200).json({status: 'Information found'})
.then(data => console.log(data.status)) // Information found
res.status(404).json({error: 'No information found'});
.catch(error => console.log('Error from backend', error))
axios.post('https://firebaselink.net/getTestData', { id: orderNumber })
.then(data => console.log(data.status))
.catch(error => console.error(error))
export const getTestData = functions.https.onRequest((req, res) => {
cors(req, res, () => {
const id = req.body.id;
admin.database().ref(`data/${id}`).once('value')
.then(snapshot => {
if(snapshot) res.status(200).json({status: "Information found"})
else res.status(404).json({error: "Information not found"})
}).catch(error => res.status(500).json({error: 'Internal error}))
})
})
Q:我尝试了error.error,但没有定义。如何打印我的错误 A:
Javascript
是非类型化的,这意味着对象可以是任何东西。这里最好的解决方案是执行console.log(JSON.stringify(error,null,2)
,查看它有什么属性,然后访问以打印消息
错误
对象通常具有消息
属性
参考资料:
前端承诺是如何创建的?答案将取决于您用于发出请求的库。您正在调用什么。然后()打开?@Hodrobond不知道您正在使用什么浏览器,但console.log(a)
可以很好地让meNot打断您的问题,但您似乎没有向响应正文中添加任何无法从响应状态推断的额外信息,例如404=“未找到”、500=“内部服务器错误”等和console.error(error)
(或者正如OP所说的console.log('error from backend',error)
)应该产生同样多的信息(如果不是更多的话)比将对象放入JSON.stringify
FYI@Phil,初始发布不包含代码console.error
,console.log
,console.error
,这无关紧要;后者只是让它变得不合理。初始发布关于error.error
,这导致error
属性b甚至未定义。因此,在不知道console.log
或console.error
的情况下,检查对象属性以查看存在什么的解决方案已被使用。抱歉,我不理解您的评论。只是为了澄清,OP已console.log('error from backend',error')
。这将显示至少与console.log(JSON.stringify(error,null,2))
相同的数据量,但也将包括任何方法和瞬态数据。因此,您的答案提供的信息比OP已经知道的要少