Reactjs 如何使用axios从json获取响应数据

Reactjs 如何使用axios从json获取响应数据,reactjs,django-rest-framework,Reactjs,Django Rest Framework,如何使用axios从json获取响应数据和状态头 我的axios代码有什么问题 我的构造函数: class DetailProduct extends React.Component{ constructor(){ super(); this.state = { dataOperations: [], isLoading: true }; console.log(this.state.idf

如何使用axios从json获取响应数据和状态头

我的axios代码有什么问题

我的构造函数:

class DetailProduct extends React.Component{

    constructor(){
        super();
        this.state = {
          dataOperations: [],
        isLoading: true
        };
      console.log(this.state.idf)

    axios.post('http://127.0.0.1:8000/barang/select/', { id: 1 })
    .then(function(response){
      console.log(response.data)
      console.log(response.status)
    });  
    }
my Django rest框架:

class Selectitem(GenericAPIView):

    permission_classes = []

    def post(self, request):

        getb = Barang.objects.filter(pk = request.POST.get('id'))
        if getb.exists():
            f = getb.first()
            return Response({'nama_barang': f.nm_barang, 'harga_satuan': f.harga_satuan}, status=status.HTTP_200_OK)
        else:
            return Response({"message": "What you were looking for isn't here."}, status=status.HTTP_400_BAD_REQUEST)
    def get_serializer_class(self):
        return SelectItem
如果使用邮递员,我可以获取数据

试着这样做

const URL = `SOME_API_URL_THAT_YOU_TYPE_HERE`;
return axios({
  method: 'POST',
  url: URL,
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${YOUR_TOKEN}`
  },
  data: {
    payload1: 'lorem',
    payload2: 'ipsum'
  },
})
.then(res => {
  console.log('Data', res.data);
 })
.catch(error => {
  console.log('Error', error.response.data);
 })

您是否收到任何错误?是的,“xhr.js:178 xhr加载失败:POST”。“在我的控制台中,执行此操作时,您将了解它是否出现了一些错误
axios.POST('http://127.0.0.1:8000/barang/select/然后(函数(响应){console.log(响应);}).catch(函数(错误){console.log(错误);});
我已经在这里回复了axios的帖子,希望它能帮助你