0

Keep getting the mistake above, even though I've searched this site and tried everything. Would really appreciate some help. The problem is with the Jquery/JSON request on the AddUser page. I have added both Jackson jar files (core and mapping.) It is deployed on Tomcat 7.

This is the configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- Application Message Bundle -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" />
        <property name="cacheSeconds" value="3000" />
    </bean>   

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="ajaxjqjsonbowling" />

    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

This is the controller:

@RequestMapping(value="/AddUser.htm", method=RequestMethod.POST)
    public @ResponseBody JsonResponse addUser(@ModelAttribute (value="player") Player player, BindingResult result){
        JsonResponse res = new JsonResponse();
        ValidationUtils.rejectIfEmpty(result, "name", "Name can not be empty.");
        if(!result.hasErrors()){
            htmlPlayerNo++;
            player.setHtmlPlayerNo(htmlPlayerNo);
            players.add(player);
            res.setStatus("SUCCESS");
            res.setResult(players);
        }else{
            res.setStatus("FAIL");
            res.setResult(result.getAllErrors());
        }
        return res;     
    }

These are the headers from chrome debugger:

Request URL:http://localhost:8080/ajaxjqjsbowling/AddUser.htm
Request Method:POST
Status Code:406 Not Acceptable
Request Headersview parsed
POST /ajaxjqjsbowling/AddUser.htm HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Content-Length: 15
Accept: */*
Origin: http://localhost:8080
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Content-Type: application/x-www-form-urlencoded
Referer: http://localhost:8080/ajaxjqjsbowling/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: JSESSIONID=E7FBB01B3FA25CFC091AA539EDFE3CC9
Form Dataview sourceview URL encoded
name:John Horan


Response Headersview parsed
    HTTP/1.1 406 Not Acceptable
    Server: Apache-Coyote/1.1
    Content-Type: text/html;charset=utf-8
    Content-Length: 1067
    Date: Mon, 06 May 2013 00:24:53 GMT

This is my JQuery code:

function doAjaxPost() {  
      // get the form values  
      var name = $('#name').val();

      $.ajax({  
        type: "POST",  
        url: contexPath + "/AddUser.htm",  
        data: "name=" + name,  
        success: function(response){
          // we have the response 
          if(response.status == "SUCCESS"){
              userInfo = "<ol>";
              for(var i =0; i < response.result.length ; i++){
                  userInfo += "<br><li><b>Name</b> : " + response.result[i].name;
              }
              userInfo += "</ol>";
              $('#info').html("User has been added to the list successfully. " + userInfo);
              $('#name').val('');         
              $('#error').hide('slow');
              $('#info').show('slow');
          }else{
              errorInfo = "";
              for(i =0 ; i < response.result.length ; i++){
                  errorInfo += "<br>" + (i + 1) +". " + response.result[i].code;
              }
              $('#error').html("Please correct following errors: " + errorInfo);
              $('#info').hide('slow');
              $('#error').show('slow');
          }       
        },  
        error: function(e){ 
              alert('Error '+ e);
        }  
      });  
    }

AddUser.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>    

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<style>

h1 {
    font-style: italic;
    color: green;
    font-family: Arial, Helvetica, sans-serif;
    }

* {
    color: blue;
    font-family: Arial, Helvetica, sans-serif;
}

</style>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ajax Jquery Bowling Game</title>

<script src="<%=request.getContextPath() %>/js/jquery.js"></script>

<script type="text/javascript">
    var contexPath = "<%=request.getContextPath() %>";
</script>

<script src="<%=request.getContextPath() %>/js/player.js"></script>

<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/style/app.css">
</head>

<body>

<div id = "topbox" style="width: 900x; height: 75px; background-color : #B7F39F">
<span id = "title"> Ajax-JQuery Bowling</span>
</div>

<h1>Welcome to The Bowling App!</h1>
    <table>
        <tr><td colspan="2"><div id="error" class="error"></div></td></tr>
        <tr><td>Enter your name : </td><td> <input type="text" id="name"><br/></td></tr>
        <tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr>
        <tr><td colspan="2"><div id="info" class="success"></div></td></tr>
    </table>
<form:form method="post" action="/ajaxjqjsbowling/showPlay.htm">
    <tr>
        <td colspan="2">
            <input type="submit" name = "button" value="Play"/>
        </td>
    </tr>
</form:form>
</body>
</html>
jchoran
  • 117
  • 2
  • 5
  • 12

1 Answers1

0

i did code for returning json but in String data type format like this :

[
    {
        "name": "Jason",
        "number": "10"
    },
    {
        "name": "Jimmy",
        "number": "11"
    }
]

take a look for json format here

And function roughly as follow :

@RequestMapping(value = "product", method = RequestMethod.GET, headers = "Accept=*/*")
public @ResponseBody
String addUser(@RequestParam("term") String query) {
    String json = "[{\"name\": \"Jason\",\"number\": \"10\" }, {\"name\": \"Jimmy\",\"number\": \"11\" }]";
    System.out.println(json);
    return json;
}

i dont use jackson or jar for that. Sorry i just know my way, may be others can explain more clearly.

Daniel Robertus
  • 1,100
  • 1
  • 11
  • 24