3

I know its a very basic question but after wasting my whole day I am asking this. I am just sending data using following AngularJS code to Django:

$http.post('/data/creation', 
                    {
                html: 'a'

            }).
              success(function(data, status, headers, config) {
                // this callback will be called asynchronously
                // when the response is available
                  console.log(data);
                  console.log(status);
                  console.log(headers);
                  console.log(config);
              }).
              error(function(data, status, headers, config) {
                // called asynchronously if an error occurs
                // or server returns response with an error status.
                  console.log(status);
                  console.log(data);
              });

and in django:

@csrf_exempt
def snippets_post(request):
    html = False
    css = False
    js = False
    JSONdata = False
    response = "You're looking at the results of question %s."
    if request.method == 'POST':
        try:
            JSONdata = request.POST.get('data', False) # it was [] in actual
        except:
            JSONdata = 'ERROR'

    return HttpResponse(JSONdata)

I am getting False as response, "by replacing data to html in POST.get result is same". I don't know whats going wrong here. Can any one help me here on this?

Thanks

Arslan Rafique
  • 176
  • 1
  • 12
  • Could you please return a static value and check.. return HttpResponse("post value") ..I am doubting about that URL is hitting this snippet or somewhere? – Asik Nov 04 '14 at 19:16
  • Communication is going perfect, I am getting False because POST.get is not able to get 'data' or 'html'. Question is how to get that? – Arslan Rafique Nov 04 '14 at 19:21
  • Please track the request using firebug or chrome network tab by clicking F12 and see if url contains html/data while request processing..if it is there, then problem is in your server side code... – Asik Nov 04 '14 at 19:35
  • Off course, data is going and checked, its some python/django issue, some parsing etc. that I am missing here, but can't say. – Arslan Rafique Nov 04 '14 at 19:38
  • Remove all the lines and simply try like this request.POST['data'] ...Check this thread http://stackoverflow.com/questions/464040/how-are-post-and-get-variables-handled-in-python – Asik Nov 04 '14 at 19:42
  • actually I tried that before, both are same except one returns False and other gives Error. that's why added try catch, but didn't worked either. – Arslan Rafique Nov 04 '14 at 19:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64266/discussion-between-asik-and-arslan-rafique). – Asik Nov 04 '14 at 19:46
  • Thanks @Asik, I solved that mystery. Appreciate you help =) – Arslan Rafique Nov 05 '14 at 15:30

1 Answers1

5

Actually, when we send data from AngularJs using $http's POST, it sends the data with the content-type = "application/json" to the server. And Django doesn't understand that format. And so you can't get the sent data.

Solution is to change the content-type header by using following config:

    app.config(['$httpProvider', function ($httpProvider) {
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
}]);
Arslan Rafique
  • 176
  • 1
  • 12