Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何显示从服务器返回的json对象_Javascript_Json_Frontend_Axios_Backend - Fatal编程技术网

Javascript 如何显示从服务器返回的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'}); .

当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'});
.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已经知道的要少