0

Experiencing an issue with AngularJS $http.post. After completing the post requests, the form instead of returning the JSON response, it instead redirects to 127.0.0.1:8000/api/forms/create. The backend response is done with Django Rest Framework. This is a CORs request. The angularJS form is hosted on port 9000 making a call to port 8000. When the server is turned off, instead of returning the error alert, it insteads returns a page not found and the url is once again redirected to 127.0.0.1:8000/api/forms/create. Appreciate any advice.

Below is the controller code for Angular script:

'use strict';
angular.module('myApp')
  .controller('DemoController', function ($scope, $http, DemoService, Tools) {
  var form_submit = function() {$http.post("127.0.0.1:8000/api/forms/create", {name:$scope.applicant, date:$scope.date, address=$scope.address}).success(function(data){ alert(data); })
  .error(function(error){
    alert('something failed');
  })};
  });

Below is the index.html:

    <!DOCTYPE html>
    <head>
    </head>
   <body ng-app="myApp" ng-submit="form_submit()">
    <FORM action="http://127.0.0.1:8000/api/forms/create/" method="post">
   <INPUT type="text" ng-model="applicant" name="name">
    <INPUT type="text" ng-model="date" name="date"> 
    <INPUT type="text" ng-model="address" name="address">
   <INPUT type="submit" value="Send"> 
    </FORM>
    </body>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script>
    <script src="controllers/demo.js"></script>
    </html>

3 Answers 3

1

Try to add / slash at the end of POST url (see the APPEND_SLASH setting for reference):

$http.post("127.0.0.1:8000/api/forms/create/"...
Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried this:

<FORM ng-submit="form_submit()">

instead of

<body ng-submit="form_submit()">

It should (hopefully) post your $http in the background

2 Comments

and Srujan Routhu is right: $scope.form_submit= function(){...}
Thank you. This was the issue that I was having. Appreciate all the help everyone.
0

your form_submit variable in the controller is not a scope variable.

Instead of using

var form_submit

change it to

$scope.form_submit

I think this will work.

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.