2017-04-18 4 views
0

Ich habe versucht, die Daten unter Verwendung von Winkeln $ http.post Verfahren in django zu schreiben, wollte ich wissen, wie ich in meiner django Ansicht, den Parameterwert zugreifen: -Wie Zugriff Winkelübergebenen Parameter in django Ansichten

Hier mein controller.js ist

var nameSpace = angular.module("ajax", ['ngCookies']); 

nameSpace.controller("MyFormCtrl", ['$scope', '$http', '$cookies', 
function ($scope, $http, $cookies) { 
    $http.defaults.headers.post['Content-Type'] = 'application/json'; 
    // To send the csrf code. 
    $http.defaults.headers.post['X-CSRFToken'] = $cookies.get('csrftoken'); 

    // This function is called when the form is submitted. 
    $scope.submit = function ($event) { 
     // Prevent page reload. 
     $event.preventDefault(); 
     // Send the data. 
     var in_data = jQuery.param({'content': $scope.card.content,'csrfmiddlewaretoken': $cookies.csrftoken}); 
     $http.post('add_card/', in_data) 
      .then(function(json) { 
      // Reset the form in case of success. 
      console.log(json.data); 
      $scope.card = angular.copy({}); 
     }); 
    } 
}]); 

Hier ist, was ich für den Zugriff bin versucht, die in meiner view-Funktion: - mein Modell des

card.content =request.POST.get('content') 

wo Kartenobjekt ist, aber ich bin immer Nameerror: name ‚con Zelt 'ist nicht definiert, bitte hilf mir!

Antwort

0

Können Sie versuchen, ohne die Jquery Param verwenden. Versuchen Sie, das einfache Objekt in dem $ http.post (url, Daten, config) passieren

var nameSpace = angular.module("ajax", ['ngCookies']); 

nameSpace.controller("MyFormCtrl", ['$scope', '$http', '$cookies', 
function ($scope, $http, $cookies) { 
    $http.defaults.headers.post['Content-Type'] = 'application/json'; 
    // To send the csrf code. 
    $http.defaults.headers.post['X-CSRFToken'] = $cookies.get('csrftoken'); 

    // This function is called when the form is submitted. 
    $scope.submit = function ($event) { 
     // Prevent page reload. 
     $event.preventDefault(); 
     // Send the data. 
     var config = { 
      headers : { 
       'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;' 
      } 
     } 
     var in_data = {content: $scope.card.content,csrfmiddlewaretoken: $cookies.csrftoken}; 
     $http.post('add_card/', in_data, config) 
      .then(function(json) { 
      // Reset the form in case of success. 
      console.log(json.data); 
      $scope.card = angular.copy({}); 
     }); 
    } 
}]); 
+0

soll ich meinen views.py ändere auch ?? @ agam –

+0

@VinitRaj keine Notwendigkeit, die view.py Datei zu ändern –

Verwandte Themen