Using axios with Django

I was trying to use axios with django but my request.POST kept turning up empty. Doing the same using jQuery/ajax-old-style worked just fine. After a few hours, including a desperate attempt at using Fetch API instead of axios, I finally found the answer in a dark corner of the web:

         var params = new URLSearchParams();
            params.append('processor_uuid', $('#uuid').val());
            params.append('rfid_token', $('#rfid_token').val());

         axios.post(url, params)
          .then(function (response) {
            console.log(response.data);
              $('#result').replaceWith(response.data);
          })
          .catch(function (error) {
            console.log(error);
          });

Just passing my params as a dictionary to axios.post didn't work. It had to be in the form of an URLSearchParams-instance. Whatever that is. But at least now my request.POST had some data in it when landing in my API :-)

Show Comments