keep getting mistake above, though i've searched site , tried everything. appreciate help. problem jquery/json request on adduser page. have added both jackson jar files (core , mapping.) deployed on tomcat 7.
this 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 classpath of application @components deploy beans --> <context:component-scan base-package="ajaxjqjsonbowling" /> <!-- configures @controller programming model --> <mvc:annotation-driven /> <!-- resolves view names protected .jsp resources within /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 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 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 headers 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, 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 jquery code:
function doajaxpost() { // form values var name = $('#name').val(); $.ajax({ type: "post", url: contexpath + "/adduser.htm", data: "name=" + name, success: function(response){ // have response if(response.status == "success"){ userinfo = "<ol>"; for(var =0; < response.result.length ; i++){ userinfo += "<br><li><b>name</b> : " + response.result[i].name; } userinfo += "</ol>"; $('#info').html("user has been added list successfully. " + userinfo); $('#name').val(''); $('#error').hide('slow'); $('#info').show('slow'); }else{ errorinfo = ""; for(i =0 ; < 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 bowling app!</h1> <table> <tr><td colspan="2"><div id="error" class="error"></div></td></tr> <tr><td>enter 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>
i did code returning json in string data type format :
[ { "name": "jason", "number": "10" }, { "name": "jimmy", "number": "11" } ] take json format here
and function 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 that. sorry know way, may others can explain more clearly.
Comments
Post a Comment