I'm moving from Jquery AJAX to Axios since I'm using ReactJS so I think it's cleaner, I am having some troubles posting a simple request to the server, the post method goes through my view but whenever I print(request.POST)
, I have an empty queryset (<QueryDict: {}>
).
axios({
method: 'post',
url: SITE_DOMAIN_NAME + '/my_url_name/',
data: {
'tes1':'test',
'tes2':'test'
},
headers: {
"X-CSRFToken": CSRF_TOKEN,
"content-type": "application/json"
}
}).then(function (response) {
console.log(response)
}).catch(function (error) {
console.log(error)
});
Django view is a simple ClassBasedView.
What am I doing wrong?
request.POST
is only for form-encoded data. If you are posting JSON, then you should use request.body
instead.
import json
json.loads(request.body.decode('utf-8'))
If you do this, you'll have to make changes to your class based view to use request.body
instead.
If you want to get axios to send form encoded data instead, this issue on GitHub might help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With