hi i am creating a application with Angularjs,REST service with spring. i want to pass the object from angulajs url to rest service , but it does work, please any one help me, my jsp page code is like below,

<html ng-app="studentApp">
<body>
<div ng-controller="studentController">
<table border = "0">
<tr>
           <td>Enter first name:</td>
           <td><input type = "text" ng-model = "student.firstName"></td>
        </tr>

        <tr>
           <td>Enter last name: </td>
           <td>
              <input type = "text" ng-model = "student.lastName">
           </td>
        </tr>
</table>
</div>
</body>
</html>

and my angularjs code is,

var studentApp = angular.module("studentApp", []);

  studentApp.config(['$httpProvider', function ($httpProvider) {
  $httpProvider.defaults.useXDomain = true;
  delete $httpProvider.defaults.headers.common['X-Requested-With'];
   }]);

 studentApp.controller("studentController", [ '$scope', '$http',
 function($scope, $http) {

$scope.toggle=true;
var urlBase="http://localhost:8080/studentweb/";        

    $scope.insertStudent = function inserStudent() {

        $http({
            method : 'POST',
            url : urlBase+'/Student/insert',
            data:  {student:data}
        }).success(function(data, status, headers, config) {
            $scope.student=data;
            $scope.toggle='!toggle';
        }).error(function(data, status, headers, config) {
            alert( "failure1");
        });

and my rest service is,

 public class StudentController
{
 @RequestMapping(value="/Student/insert",method = RequestMethod.POST ,params= {"student"})
 public String insertStudent(@RequestParam("student") StudentVO student) throws ParseException {    

     student.setFirstName(student.getFristName());  
     student.setLastName(student.getLstName());     
     studentcontrol.addStudent(student);    

     return "";

 }  

}
} ])
share|improve this question
    
I guess $.params is jQuery. And I haven't found any such function in the documentation. Also, there is no ng-app in the posted HTML. Post a minimal complete example reproducing the problem, and tell how it doesn't work. Open your browser console and check for error messages. Why doesn't your REST API accept JSON? It would make things much easier. – JB Nizet Nov 6 '15 at 7:06
    
the alert message failure1 is coming. there is no error log on the console – premashree Nov 6 '15 at 7:15
    
You're now sending a JSON object in the body of the request, as JSON, to a REST service not expecting JSON. – JB Nizet Nov 6 '15 at 11:20
    
could you please tell me how to send the JSON object to a REST service – premashree Nov 7 '15 at 7:48

The problem is that you "usrlBase" variable has "student/" extra as you are already calling your Student controller in url : urlBase+'/Student/insert' Hence the complete URL becomes something like http://localhost:8080/student//Student/insert whereas it should be something like: http://localhost:8080/Student/insertStudent

Update: Below is an absolutely fine working example with a sample restful service you had some brackets missing in your code. Please go through the below code and get back to me if required.

<html ng-app="studentApp">
<div ng-controller="studentController">
    <table border="0">
        <tr>
            <td>Enter first name:</td>
            <td><input type="text" ng-model="student.FirstName"></td>
        </tr>
        <tr>
            <td>Enter last name: </td>
            <td>
                <input type="text" ng-model="student.LastName">
            </td>
        </tr>
    </table>
</div>

Script: var studentApp = angular.module("studentApp", []);

    studentApp.config(['$httpProvider', function ($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }]);

    studentApp.controller("studentController", ['$scope', '$http',
    function ($scope, $http) {

        $scope.toggle = true;
        //var urlBase = "http://localhost:8080/student/";

       // $scope.insertStudent = function () {
            debugger;
            $http({
                method: 'GET',
                url: 'http://services.odata.org/V4/Northwind/Northwind.svc/Employees(1)?$format=json',
                // data: { student: data }

            }).success(function (data, status, headers, config) {
                debugger;
                $scope.student = data;
                $scope.toggle = '!toggle';
            }).error(function (data, status, headers, config) {
                debugger;
                alert("failure1");
            });
        //  }
    }]);
share|improve this answer
    
I changed the url but it is not working at all – premashree Nov 6 '15 at 9:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.