0

when i submit signup form using postman it works fine, but when i try to submit using the below code it shows some error, im unable to figure it out, anyone please help me?

.controller('RegisterCtrl', function($scope, AuthService, $state, $http) {
  $scope.user = {
    name: '', 
    mobile:'',
    email:'',
    password:''
  };

  $scope.signup = function() {
    $http.post("http://localhost:8080/api/signup", $scope.user, {headers: {'Content-Type': 'application/json'} })
        .then(function (response) {
            return response;
        });
  };
})

when i inspect chrome browser it logs the below error:

angular.js:14362 Error: Unexpected request: POST http://localhost:8080/api/signup
No more request expected
    at $httpBackend (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-mocks.js:1402:9)
    at sendReq (http://localhost:3000/bower_components/angular/angular.js:12178:9)
    at serverRequest (http://localhost:3000/bower_components/angular/angular.js:11930:16)
    at processQueue (http://localhost:3000/bower_components/angular/angular.js:16689:37)
    at http://localhost:3000/bower_components/angular/angular.js:16733:27
    at Scope.$eval (http://localhost:3000/bower_components/angular/angular.js:18017:28)
    at Scope.$digest (http://localhost:3000/bower_components/angular/angular.js:17827:31)
    at ChildScope.$apply (http://localhost:3000/bower_components/angular/angular.js:18125:24)
    at HTMLInputElement.<anonymous> (http://localhost:3000/bower_components/angular/angular.js:26813:23)
    at HTMLInputElement.dispatch (http://localhost:3000/bower_components/jquery/dist/jquery.js:4435:9) Possibly unhandled rejection: {}(anonymous function) @ angular.js:14362(anonymous function) @ angular.js:10859processChecks @ angular.js:16715$eval @ angular.js:18017$digest @ angular.js:17827$apply @ angular.js:18125(anonymous function) @ angular.js:26813dispatch @ jquery.js:4435elemData.handle @ jquery.js:4121  
Inayath
  • 99
  • 9
  • What is the error you are having ? You can log you error in the error callback funtion – Lotus91 Mar 15 '17 at 13:59
  • it's impossible to know what your issue is without knowing what the error is, but one of the most common issues you might have with this type of code is CORS. maybe this will help? http://stackoverflow.com/questions/17756550/angularjs-cors-issues – Claies Mar 15 '17 at 14:01
  • Have you tried sending your data like this `JSON.stringify($scope.user)` ? – Lotus91 Mar 15 '17 at 14:02
  • You have to let us know what the error is. without that its going to be difficult to help – Kxng Kombian Mar 15 '17 at 14:04
  • im really sorry! i have just updated the question with error. @Lotus91 i have tried it didnt worked. – Inayath Mar 15 '17 at 14:17
  • do you have the `angular-mocks` library installed? It looks like your app *thinks* you want to test `$http` through `$httpBackend` instead of sending a request to the server, but you don't have a test set up for it. – Claies Mar 15 '17 at 14:20
  • @Claies how to test set up for it? please can u help me out. i tried to uninstall angular-mocks bower component but still same error. – Inayath Mar 15 '17 at 14:28
  • are you **trying** to test `$http`, or are you wanting to send your request to the server? because at the moment, you are asking why your app can't get to the same thing that postman can, but it appears like you specifically installed a module that causes that behavior. – Claies Mar 15 '17 at 14:30
  • did you remove the `ngMocks` dependency in your app when you tried to uninstall it? you don't have enough code in the question to really give an accurate answer at this point. – Claies Mar 15 '17 at 14:36
  • @Claies hey thanks, actually i m making call to server and i removed all dependencies of ngMocks, but still error exist. – Inayath Mar 15 '17 at 14:46
  • can you post the updated error? because you **can't** be seeing an error from `$httpBackend` if you don't have `ngMocks` installed, so the error won't be **exactly** the same.... – Claies Mar 15 '17 at 14:48
  • @Claies hey thanks man, now it's working but now values are not storing in mongoDB. api log is : OPTIONS /api/signup 204 8.715 ms - - POST /api/signup 200 5.445 ms - 56 i handled cors at server end. – Inayath Mar 15 '17 at 14:51
  • api log: when i print req.body i m getting following object -- { '{"name":"rahul jain","mobile":"343453","email":"inayath@gmail.in","password":"123","cpassword":"123"}': '' } – Inayath Mar 15 '17 at 15:04

1 Answers1

0

As already pointed out, you're lacking a lot of important details in your description. My best guess is that your endpoint is expecting your user object to be named "user", in which case your post call should look more like the following:

$http.post(
    "http://localhost:8080/api/signup",
    { user: $scope.user }, 
);
Jeff Howard
  • 334
  • 2
  • 7