我搜索网络这个话题,但我不能让那个工作为例。 我将gladed与有人可以给我一个帮助。
I search this topic on web but I can't get a example that worked. I will be gladed with someone could give me a help.
这是我的测试。
$.ajax({ url: 'GetJson', type: 'POST', dataType: 'json', contentType: 'application/json', data: {id: 'idTest'}, success: function(data) { console.log(data); } });在Sevlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String id2[] = request.getParameterValues("id"); String id3 = request.getHeader("id"); }
我收到空的一切。
I'm getting null in everything.
推荐答案排序的答案是,这个数据被隐藏在请求的InputStream 。
The sort answer is that this data is hidden in the request InputStream.
下面的servlet是如何使用该演示(我在的JBoss 7.1.1运行它):
The following servlet is a demo of how you can use this (I am running it on a JBoss 7.1.1):
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.URLDecoder; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name="fooServlet", urlPatterns="/foo") public class FooServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { InputStream is = req.getInputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buf = new byte[32]; int r=0; while( r >= 0 ) { r = is.read(buf); if( r >= 0 ) os.write(buf, 0, r); } String s = new String(os.toByteArray(), "UTF-8"); String decoded = URLDecoder.decode(s, "UTF-8"); System.err.println(">>>>>>>>>>>>> DECODED: " + decoded); System.err.println("================================"); Enumeration<String> e = req.getParameterNames(); while( e.hasMoreElements() ) { String ss = (String) e.nextElement(); System.err.println(" >>>>>>>>> " + ss); } System.err.println("================================"); Map<String,String> map = makeQueryMap(s); System.err.println(map); ////////////////////////////////////////////////////////////////// //// HERE YOU CAN DO map.get("id") AND THE SENT VALUE WILL BE //// //// RETURNED AS EXPECTED WITH request.getParameter("id") //// ////////////////////////////////////////////////////////////////// System.err.println("================================"); resp.setContentType("application/json; charset=UTF-8"); resp.getWriter().println("{'result':true}"); } // Based on code from: www.coderanch/t/383310/java/java/parse-url-query-string-parameter private static Map<String, String> makeQueryMap(String query) throws UnsupportedEncodingException { String[] params = query.split("&"); Map<String, String> map = new HashMap<String, String>(); for( String param : params ) { String[] split = param.split("="); map.put(URLDecoder.decode(split[0], "UTF-8"), URLDecoder.decode(split[1], "UTF-8")); } return map; } }通过请求:
$.post("foo",{id:5,name:"Nikos",address:{city:"Athens"}})
的输出是:
>>>>>>>>>>>>> DECODED: id=5&name=Nikos&address[city]=Athens ================================ ================================ {address[city]=Athens, id=5, name=Nikos} ================================
(注: req.getParameterNames()不起作用印在4号线的地图包含了所有的数据通常可使用请求。 。的getParameter()还要注意嵌套对象表示法, {地址:{城市:雅典}} &RARR; 地址[城市] =雅典)
(NOTE: req.getParameterNames() does not work. The map printed in the 4th line contains all the data normally accessible using request.getParameter(). Note also the nested object notation, {address:{city:"Athens"}} → address[city]=Athens)
略无关的问题,但出于完整性的:
Slightly unrelated to your question, but for the sake of completeness:
如果你想使用一个服务器端的JSON解析器,你应该使用 JSON.stringify 的数据:
If you want to use a server-side JSON parser, you should use JSON.stringify for the data:
$.post("foo",JSON.stringify({id:5,name:"Nikos",address:{city:"Athens"}}))在我看来,与服务器进行通信JSON的最好方法是使用JAX-RS(或者Spring同等学历)。它是现代服务器死了简单而解决这些问题。
In my opinion the best way to communicate JSON with the server is using JAX-RS (or the Spring equivalent). It is dead simple on modern servers and solves these problems.
更多推荐
在的Java Servlet通过jQuery AJAX GET参数发送
发布评论