简体   繁体   中英

AngularJs controller get request not hitting Spring MVC Rest Service

So I have my angular javascript as

    var app = angular.module('app', []);
    app.controller('controller', function($scope, $http) {
        $http.get('http://localhost:8080/core/students.json')
                .success(function(data) {
                    $scope.user = data;
                });
    });

and my rest controller with

@RestController
public class StudentRestController {

@RequestMapping(value = "/students", produces = { "application/json" }, method =      RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public Student getStudent() {
    // return studentService.getStudentByUserName(userName);
    Student s = new Student();
    s.setUserName("userName");
    s.setEmailAddress("email");
    return s;
}
}

but for some reason, the javascript ajax request isn't hitting the method getStudent(). Why is this? I get a console error

"GET http://localhost:8080/core/students.json 404 (Not Found)"

ordinary button url calls work as expected

change angularjs app as

var app = angular.module('app', []);
app.controller('controller', [ "$scope", "$http", function($scope, $http) {
    $http.get("http://localhost:8080/students").success(function(data) {
        $scope.user = data;
    });
} ]);

that holds if ur web.xml

<servlet-mapping>
        <servlet-name> dispatcherServlet </servlet-name>
        <url-pattern> * </url-pattern>
    </servlet-mapping>

您在@RequestMapping上定义“ / students”,那么您的URL应为“ ... / core / students”。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM