-1

how i get response data and status header from json with axios?

whats wrong with my axios code?

my constructor:

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 framework:

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

i can get data if using postman

postman response data and status

  • Are you getting any error? – Shubham Khatri Dec 12 '17 at 08:37
  • yes, "xhr.js:178 XHR failed loading: POST "http://127.0.0.1:8000/barang/select/"." in my console – Arnold Starscream Dec 12 '17 at 08:42
  • Try doing this you will get to know if it is giving some error `axios.post('http://127.0.0.1:8000/barang/select/', { id: 1 }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });` – pritesh Dec 12 '17 at 08:54
  • I have answered axios post here, Hope it will help you https://stackoverflow.com/questions/47630163/axios-post-request-to-send-form-data/47630754#47630754 – Aaqib Dec 12 '17 at 10:09

1 Answers1

0

Try doing it like this

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);
 })
Adeel Imran
  • 13,166
  • 8
  • 62
  • 77