0

I am new beginner for Angular JS. I am trying to submit data from jsp page using angular js to spring mvc controller. I am trying to inject data to Pojo Object. But i am getting exception saying '415 unsupported'. Please help me where i went wrong .

JSP Page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Testing Tool</title>
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script type="text/javascript">
var apps=angular.module('sampleApps',[]);
apps.controller('formController',function($scope, $http)
        {

    $scope.register=function()
    {
        var name=$scope.name;
        var address=$scope.address;
        alert(name + " , "+address);
        var formData = {
                  name       : name,
                  address    : address
              };
         $http({
                method : "POST",
                url : "userdetails",
                contentType: 'application/json',
                mimeType: 'application/json',
                /*   data: {name: name,address:address} */  
                 data:formData
                }).then(function mySucces(response) {
                 //$scope.myWelcome = response.data;
                 alert("Success");
                }, function myError(response) {
                    alert(response);
                 // $scope.myWelcome = response.statusText;
              });   

    };

        });
</script>
</head>
<body data-ng-app="sampleApps">
 <div  ng-controller="formController">
        Name: <br/>
        <input type="text" name="name" ng-model="name"/><br/>
        Address: <br/>
        <input type="text" name="address" ng-model="address"/><br/>

        <input type="button" value="submit"  ng-click="register()">
 </div>
</body>
</html>

Controller:

@RequestMapping(value="/userdetails")
    public @ResponseBody User getUserDetails(HttpServletRequest request,@RequestBody User data)
    {
        System.out.println(data);
        return data;
    }

Pojo Class:

public class User implements Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private String name;
    private String address;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}
Navyah
  • 1,660
  • 10
  • 33
  • 58
  • 1
    Try using Angular's `$http.post` shorthand instead of the longer notation that you're using. You might be messing it up by providing the `content-type` or `mime-type` manually. – glcheetham Mar 31 '16 at 07:49
  • Even if i use short cut also , i am getting same exception – Navyah Mar 31 '16 at 08:33

2 Answers2

0

I think you need to add data:formData => data:JSON.stringify(formData)

Check whether this helps you or not

Cheers!

Chetan Aher
  • 164
  • 1
  • 7
  • Hi Chetan, I tried by adding data:JSON.stringify(formData). but still it didnt worked out. I added the jars jackson-jaxrs-1.9.2.jar jackson-xc-1.9.2.jar jersey-client-1.18.jar jersey-json-1.18.jar jersey-server-1.18.jar jersey-core-1.18.jar jersey-servlet-1.18.jar jsr311-api-1.1.1.jar – Navyah Mar 31 '16 at 09:10
  • please check from this url http://stackoverflow.com/questions/17560258/pass-array-data-from-javascript-in-browser-to-spring-mvc-controller-using-ajax/36174953#36174953 I have passed there an string array you can replace replace it with your object and also for safer side keep both object same name as user – Chetan Aher Apr 01 '16 at 07:01
0

Based on your tag, I suposed you are using Spring MVC. Just add a new loggger for the spring web package. Raising the log level of the package org.springframework.web to DEBUG will give you the root cause of the 415 error.

Here is an exemple using logback :

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <logger name="org.springframework.web" level="DEBUG" />

    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

You'll also have a trace of all the request mapping resolved by your HTTP request.

Good luck.

Daniel Lavoie
  • 1,852
  • 1
  • 16
  • 19