0

I want to make search by AngularJS and spring MVC but the following code didn't work There are no errors in eclipse console or web console

This is Spring MVC Controller

@RequestMapping(value = "app/rest/contacts/search",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<Contact> find(@RequestParam(value = "name") String name) {
    List<Contact> queryResults = contactRepository.search(name);
    return queryResults;
}

This is AngularJS Service

 pagingpocApp.factory('Contact', function ($http) {
 return {
        search: function(name) {
            var promise = $http.get('app/rest/contacts/search',{params: {name: name}}).then(function (response) {
                return response.data;
            });
            return promise;
        }
 }
  });

this is AngularJS Controller

pagingpocApp.controller('ContactController', function ($scope, $filter,resolvedContact, Contact) {
$scope.search= function() {
    Contact.search($scope.name).then(function(obj) {
        console.log(obj)
                  });
    }
   });

Html Page

<input ng-model="name">
<input type="submit" ng-click="search()">
1
  • Use your browser's debugging tool to watch the network traffic, turn up Web MVC logging to DEBUG and see if the requests are going through, and make sure that if that's not a @RestController you use @ResponseBody. Commented Jan 20, 2015 at 13:44

2 Answers 2

1

Your code is correct There are no errors ,Make sure you not use function name more than one, may you use search name in two function in angular controller

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

Comments

0

Try this,

Change this line to

var promise = $http.get('app/rest/contacts/search',{params: {name: name}}).then(function (response) {
            return response.data;
        });

to

var promise = $http.get('app/rest/contacts/search',{params: {name: name}});

Then in ContactController, doing obj.data should give you the data.

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.