1

i have started AngularJS 4 days ago and this question is answered many times but i cant correct my problem, here us my code

userType.html

<div ng-controller="formuserTypesController">
        User Type: <input type="text" ng-model="title" style="margin-bottom: 10px;"><br/>
        Description: <input type="text" ng-model="description" style="margin-bottom: 10px;"><br/>
        <button ng-keyup="add($event)" style="margin-bottom: 10px;">Submit Form</button>

        <h3>{{answer}}</h3>   
        <h3>{{displayres}}</h3>
        <br/>   
</div>

<div ng-controller="userTypesController">
    <!--div data-ng-repeat="ut in usertypes|limitTo:10 "-->
    <div data-ng-repeat="ut in usertypes ">
        <b>ID:</b> {{ ut.id }}<br/>
        <b>User Type:</b> {{ ut.usertype_title }}<br/>
        <b>Description: </b> {{ ut.usertype_desc }}<br/><br/>
    </div>
    <br/>
</div>

and controller.js

eventsApp.controller('formuserTypesController', ['$scope', '$routeParams', 
    function($scope, $routeParams, $http) {
        $scope.title = '';
        $scope.description = '';
        $scope.add = function( event ) {
            if( event.keyCode == 13 ) {
                $scope.answer = 'Posting data . . .';
                //$http.defaults.headers.post["Content-Type"] = "application/json";
                $http({
                    method: 'POST',
                    url: '/developer/user_type/',
                    dataType: 'json',
                    data: { usertype_title: $scope.title, usertype_desc: $scope.description },              
                    headers: { 'Content-Type': 'application/json; charset=UTF-8' },
                    }).
                    success(function(data, status, headers, config) {
                        $scope.displayres = data
                        $scope.answer = 'Data has been successfully posted.';
                        //console.log(data);
                    }).
                    error(function(data, status, headers, config) {
                        $scope.displayres = data
                        $scope.answer = 'Posting data was unsuccessful.';
                        //console.log(data);
                      });                                       
            }
        }       
}]);

i cant get it working using any solution provided on stackoverflow, any help will be appreciated

console

Quit the server with CTRL-BREAK.
[08/Dec/2014 15:58:00] "GET / HTTP/1.1" 200 1265
[08/Dec/2014 15:58:01] "GET /static/js/controllers.js HTTP/1.1" 200 2541
[08/Dec/2014 15:58:01] "GET /static/js/partials/userType.html HTTP/1.1" 304 0
[08/Dec/2014 15:58:01] "GET /developer/user_type/?format=json HTTP/1.1" 200 210
[08/Dec/2014 16:00:02] "GET /developer/user_type/ HTTP/1.1" 200 11576
[08/Dec/2014 16:36:33] "GET / HTTP/1.1" 200 1265
[08/Dec/2014 16:36:33] "GET /static/js/app.js HTTP/1.1" 304 0
[08/Dec/2014 16:36:33] "GET /static/js/controllers.js HTTP/1.1" 304 0
[08/Dec/2014 16:36:33] "GET /static/js/partials/userType.html HTTP/1.1" 200 688
[08/Dec/2014 16:36:33] "GET /developer/user_type/?format=json HTTP/1.1" 200 210
4
  • What is your back-end code? What is your response? Maybe it's just routing issue? If you get 404, it's probably routing Commented Dec 8, 2014 at 11:16
  • nothing appears get method works but post method is not even showing in console with any kind of response Commented Dec 8, 2014 at 11:39
  • Does your console show the out going POST request actually being made? Commented Dec 8, 2014 at 11:50
  • only GET mehtods are listed POST method doesnt appear. Commented Dec 8, 2014 at 12:22

2 Answers 2

0

It's due to CORS restrictions.

See:

Sign up to request clarification or add additional context in comments.

3 Comments

why it would be a cors issue?
$http.get method is working fine, but POST method is not showing any response in console.
turning off all authentication still GET method works but POST doesn't
0

in controller.js change these two lines

eventsApp.controller('formuserTypesController', ['$scope', '$routeParams', 
    function($scope, $routeParams, $http) {

to

eventsApp.controller('formloginController', function($scope, $http) {

and also addition of csrftoken in webservice, resolved my issue.

in views.py

from django.core.context_processors import csrf

def index(request):
    c = {}
    c.update(csrf(request))
    return render_to_response('index.html', c)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.