python - Cant get AngularJS's $http.post data in Django -
I have a very basic question on this, but after wasting my entire day, I am asking. I'm just sending data to Django using the following AngularJS code:
$ http.post ('/ data / creation', {html: 'a'}). Success (function (data, position, header, config) {// This callback will be called Asynchronously / when response is available console.log (data); console.log (status); console.log (headers); console .log (config);}) Error (function (Data, Status, Header, Config) {// Asynchronous call is made if an error occurs / or the server responds with error status. Console Log (status); console.log (data);});
and in django:
@csrf_exempt def snippets_post (request): html = False CSS = false JS = false JSONdata = Wrong response = "You are seeing the result of question% s." If request.method == 'POST': try: JSONdata = request.POST.get ('data' Incorrect) # It was actually except for []: JSONdata = 'ERROR' return HttpResponse (JSONdata)
I am getting wrong as the answer, "POST.get In the result HTML is the same from the place of data ". I do not know what is going wrong here. Can anyone help me on this? In fact, when we send data from Angel J, then $ POST uses POST, then content-type = "application / json" . And Django does not understand that format and therefore you can not get the data sent.
The solution is to replace the content-type header using the following config:
app.config (['$ httpProvider', function ($ httpProvider) {$ httpProvider. Defaults.headers.post ['content-type'] = 'application / x-www-form-urlencoded';}]);
Comments
Post a Comment