Read Ajax post data in django
I got an Ajax request using promise in my django project:
var path = window.location.pathname;
fetch('/getblogs/', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({'path': path})
}).then(function (response) {
return response.json();
});
The request is in a js
file and there is no form.
I'm trying to read data in my views.py
like this:
@csrf_exempt
def get_blogs(request):
cat_id = request.POST.get('path')
print("RESULT: " + str(cat_id))
But in output I get:
RESULT: None
Am I missing somthing in reading post data or there is something wrong with my ajax request?
python ajax django es6-promise
add a comment |
I got an Ajax request using promise in my django project:
var path = window.location.pathname;
fetch('/getblogs/', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({'path': path})
}).then(function (response) {
return response.json();
});
The request is in a js
file and there is no form.
I'm trying to read data in my views.py
like this:
@csrf_exempt
def get_blogs(request):
cat_id = request.POST.get('path')
print("RESULT: " + str(cat_id))
But in output I get:
RESULT: None
Am I missing somthing in reading post data or there is something wrong with my ajax request?
python ajax django es6-promise
add a comment |
I got an Ajax request using promise in my django project:
var path = window.location.pathname;
fetch('/getblogs/', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({'path': path})
}).then(function (response) {
return response.json();
});
The request is in a js
file and there is no form.
I'm trying to read data in my views.py
like this:
@csrf_exempt
def get_blogs(request):
cat_id = request.POST.get('path')
print("RESULT: " + str(cat_id))
But in output I get:
RESULT: None
Am I missing somthing in reading post data or there is something wrong with my ajax request?
python ajax django es6-promise
I got an Ajax request using promise in my django project:
var path = window.location.pathname;
fetch('/getblogs/', {
method: 'post',
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({'path': path})
}).then(function (response) {
return response.json();
});
The request is in a js
file and there is no form.
I'm trying to read data in my views.py
like this:
@csrf_exempt
def get_blogs(request):
cat_id = request.POST.get('path')
print("RESULT: " + str(cat_id))
But in output I get:
RESULT: None
Am I missing somthing in reading post data or there is something wrong with my ajax request?
python ajax django es6-promise
python ajax django es6-promise
asked Nov 19 '18 at 8:30
Alex JoligAlex Jolig
8,3511674110
8,3511674110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I think you can try like this:
import json
@csrf_exempt
def get_blogs(request):
cat_id = json.loads(request.body).get('path')
print("RESULT: " + str(cat_id))
1
I should've gone for body not post. This solved the issue. Thanks!
– Alex Jolig
Nov 19 '18 at 8:43
add a comment |
From the Django documentation
HttpRequest.POST
A dictionary-like object containing all given HTTP POST parameters,
providing that the request contains form data. See the QueryDict
documentation below. If you need to access raw or non-form data posted
in the request, access this through the HttpRequest.body attribute
instead.
Try using json.loads(request.body)['path']
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370872%2fread-ajax-post-data-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you can try like this:
import json
@csrf_exempt
def get_blogs(request):
cat_id = json.loads(request.body).get('path')
print("RESULT: " + str(cat_id))
1
I should've gone for body not post. This solved the issue. Thanks!
– Alex Jolig
Nov 19 '18 at 8:43
add a comment |
I think you can try like this:
import json
@csrf_exempt
def get_blogs(request):
cat_id = json.loads(request.body).get('path')
print("RESULT: " + str(cat_id))
1
I should've gone for body not post. This solved the issue. Thanks!
– Alex Jolig
Nov 19 '18 at 8:43
add a comment |
I think you can try like this:
import json
@csrf_exempt
def get_blogs(request):
cat_id = json.loads(request.body).get('path')
print("RESULT: " + str(cat_id))
I think you can try like this:
import json
@csrf_exempt
def get_blogs(request):
cat_id = json.loads(request.body).get('path')
print("RESULT: " + str(cat_id))
answered Nov 19 '18 at 8:35
ruddraruddra
12.5k32648
12.5k32648
1
I should've gone for body not post. This solved the issue. Thanks!
– Alex Jolig
Nov 19 '18 at 8:43
add a comment |
1
I should've gone for body not post. This solved the issue. Thanks!
– Alex Jolig
Nov 19 '18 at 8:43
1
1
I should've gone for body not post. This solved the issue. Thanks!
– Alex Jolig
Nov 19 '18 at 8:43
I should've gone for body not post. This solved the issue. Thanks!
– Alex Jolig
Nov 19 '18 at 8:43
add a comment |
From the Django documentation
HttpRequest.POST
A dictionary-like object containing all given HTTP POST parameters,
providing that the request contains form data. See the QueryDict
documentation below. If you need to access raw or non-form data posted
in the request, access this through the HttpRequest.body attribute
instead.
Try using json.loads(request.body)['path']
add a comment |
From the Django documentation
HttpRequest.POST
A dictionary-like object containing all given HTTP POST parameters,
providing that the request contains form data. See the QueryDict
documentation below. If you need to access raw or non-form data posted
in the request, access this through the HttpRequest.body attribute
instead.
Try using json.loads(request.body)['path']
add a comment |
From the Django documentation
HttpRequest.POST
A dictionary-like object containing all given HTTP POST parameters,
providing that the request contains form data. See the QueryDict
documentation below. If you need to access raw or non-form data posted
in the request, access this through the HttpRequest.body attribute
instead.
Try using json.loads(request.body)['path']
From the Django documentation
HttpRequest.POST
A dictionary-like object containing all given HTTP POST parameters,
providing that the request contains form data. See the QueryDict
documentation below. If you need to access raw or non-form data posted
in the request, access this through the HttpRequest.body attribute
instead.
Try using json.loads(request.body)['path']
answered Nov 19 '18 at 8:36
nightgauntnightgaunt
11310
11310
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53370872%2fread-ajax-post-data-in-django%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown