Javascript React axios response.json不是函数
我一直收到一个错误,即response.json不是包含axios请求数据的函数。我不知道为什么,因为这段代码两天前起作用了。。。我试着用承诺来搞乱局面,看看这是否会有所不同,但似乎不是这样,代码在下面,任何帮助都是非常感谢的Javascript React axios response.json不是函数,javascript,reactjs,axios,Javascript,Reactjs,Axios,我一直收到一个错误,即response.json不是包含axios请求数据的函数。我不知道为什么,因为这段代码两天前起作用了。。。我试着用承诺来搞乱局面,看看这是否会有所不同,但似乎不是这样,代码在下面,任何帮助都是非常感谢的 import React, { Component } from 'react' import TrackingRender from '../TrackingRender/TrackingRender' import axios from 'axios' import
import React, { Component } from 'react'
import TrackingRender from '../TrackingRender/TrackingRender'
import axios from 'axios'
import { Redirect } from 'react-router';
export class OrderTracking extends Component {
constructor() {
super()
this.state = {
loadingData: true,
order: []
}
}
async componentDidMount() {
this._isMounted = true;
if (this.state.loadingData) {
try {
const { id } = this.props.match.params
const response = await axios.get(`http://localhost:5000/api/orders/${id}`)
const data = await response.json()
this.setState({
order: data,
loadingData: false
})
console.log(this.state)
} catch (err) {
console.log(err)
}
}
}
render() {
if (!this.state.loadingData) {
return (
<div>
< TrackingRender order={this.state.order} />
</div>
)
} else {
return (
<Redirect to='/orders' />
)
}
}
}
export default OrderTracking
import React,{Component}来自“React”
从“../TrackingRender/TrackingRender”导入TrackingRender
从“axios”导入axios
从“react router”导入{Redirect};
导出类OrderTracking扩展组件{
构造函数(){
超级()
此.state={
加载数据:true,
订单:[]
}
}
异步组件didmount(){
这个。_isMounted=true;
if(this.state.loadingData){
试一试{
const{id}=this.props.match.params
const response=等待axios.get(`http://localhost:5000/api/orders/${id}`)
const data=wait response.json()
这是我的国家({
顺序:数据,
加载数据:false
})
console.log(this.state)
}捕捉(错误){
console.log(错误)
}
}
}
render(){
如果(!this.state.loadingData){
返回(
)
}否则{
返回(
)
}
}
}
导出默认订单跟踪
使用Axios,您不需要运行response.json()
只需按原样获得原始响应即可
数据应该在
response.data中可用。使用Axios,您不需要运行response.json()
async componentDidMount() {
this._isMounted = true;
if (this.state.loadingData) {
try {
const { id } = this.props.match.params
const response = await axios.get(`http://localhost:5000/api/orders/${id}`)
const data = await response.data
this.setState({
order: data,
loadingData: false
})
console.log(this.state)
} catch (err) {
console.log(err)
}
}
}
数据应在response.data中可用。要从axios
获取数据,您需要使用response.data
async componentDidMount() {
this._isMounted = true;
if (this.state.loadingData) {
try {
const { id } = this.props.match.params
const response = await axios.get(`http://localhost:5000/api/orders/${id}`)
const data = await response.data
this.setState({
order: data,
loadingData: false
})
console.log(this.state)
} catch (err) {
console.log(err)
}
}
}
要从axios
获取数据,您需要使用response.data
async componentDidMount() {
this._isMounted = true;
if (this.state.loadingData) {
try {
const { id } = this.props.match.params
const response = await axios.get(`http://localhost:5000/api/orders/${id}`)
const data = await response.data
this.setState({
order: data,
loadingData: false
})
console.log(this.state)
} catch (err) {
console.log(err)
}
}
}
您不需要在axios
响应上调用.json()
。它已经被解析了。您可以从response.data
中获取response body的值
const response = await axios.get(`http://localhost:5000/api/orders/${id}`)
this.setState({
order: response.data,
loadingData: false
})
您不需要在axios
响应上调用.json()
。它已经被解析了。您可以从response.data
中获取response body的值
const response = await axios.get(`http://localhost:5000/api/orders/${id}`)
this.setState({
order: response.data,
loadingData: false
})