0
votes

I am new in angularJS. i am creating registration task. here if i am sending individual attribute. getting success message. but i am try to send object as json.

but getting below error

Failed to load resource: the server responded with a status of 404 (Not Found)

Client side code

function registration(user) {
            var employee={
                    name:user.firstName+" "+user.lastName,
                    email:user.emailAddress,
                    password:user.password
            };
            var data = JSON.stringify(employee);
            $http.post('http://localhost:8080/AngularJSSpring/ajax/register', data
            ).success(
                    function(data, status) {
                        defer.resolve(data);
                    });
                return defer.promise;
        }

server side code (spring mvc)

@RequestMapping(value = "/ajax/register", method = RequestMethod.POST,consumes=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public JSONResponse registration(@RequestBody Employee emp) {
        System.out.println("ajax register");
        return angularView.registration(emp);

    }

i also try by modifiying

$http.post('http://localhost:8080/AngularJSSpring/ajax/register', employee)

but not working.. please help me for this

1
Well, you problem come from the URL or the server. Not from the client side (angular). I can't test that for you, did you check the url ? Using a REST Client for example.AxelH
Solved!!!. actually problem was from server side. "@RequestBody Employee emp" is not working. i change object to String and then converted json into Object..Musaddique
Like a said, server issues. You should check how Spring works, because it is suppose to do that for you (never take the time to use it ...)AxelH

1 Answers

0
votes

The thing with Spring is that, when ever you create a new Package which as a matter includes a Controller class (with @Controller annotation), Request Handler to handle your corresponding requests (ie. request made from the client side), you will have me mention this package as a base-package attribute under <context ....../> tag in spring-servlet.xml file .

<context:component-scan  base-package="Your package Name" />

You can find this file in

WebContent/WEB-INF/spring-servlet.xml

Eventually, this would tell the Spring's Request Handler to include package's controller class to search for request too.

Presuming this is the situation you have come across, Request handler in your case is not able to find the associated request , even though the @RequestMapping method exists.

Hope this helps Cheers