hani ali
hani ali

Reputation: 39

retrieve data AngularJS and spring MVC

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()">

Upvotes: 0

Views: 2038

Answers (2)

Ali-Alrabi
Ali-Alrabi

Reputation: 1698

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

Upvotes: 1

Jaideep
Jaideep

Reputation: 1

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.

Upvotes: 0

Related Questions