用jQuery ajax时,后台无法获取前台传来的json数组,急,急,急!!!
代码如下: 望高手指点或提供一个用jQuery ajax传json数组后台解析的例子,在此感谢不尽。
前台js:
$(function(){
$("#btnOk").click(function(){
$.post("link.do?operate=parse", "[ {name:'a', gender:'男'}, {name:'b', gender:'女'} ]");
})
});
后台:
StringBuffer jsondata = new StringBuffer();
System.out.println("json数组解析开始了!!!");
String line = null;
while ((line = request.getReader().readLine()) != null) {
jsondata.append(message);
}
System.out.println("接收到的json数据:"+ jsondata.toString());
JSONArray array = new JSONArray(jsondata);
for(int i = 0; i < array.length(); i++) {
JSONObject json = (JSONObject) array.get(i);
System.out.println(json.getString("name")+ ","+ json.getString("gender"));
}
输出:
信息: Initialize action of type: com.json.struts.action.LinkAction
json数组解析开始了
接收到的json数据:
-3-2 21:27:22 org.apache.mands.AbstractExceptionHandler execute
警告: Unhandled exception
java.lang.NullPointerException
at java.io.StringReader.(StringReader.java:33)
at org.json.JSONTokener.(JSONTokener.java:66)
at org.json.JSONArray.(JSONArray.java:157)
at com.json.struts.action.LinkAction.parsejson(LinkAction.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:269)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:170)
at org.apache.mands.servlet.ExecuteAction.execute(ExecuteAction.java:58)
at org.apache.mands.AbstractExecuteAction.execute(AbstractExecuteAction.java:67)
at org.apache.mands.ActionCommandBase.execute(ActionCommandBase.java:51)
at mons.chain.impl.ChainBase.execute(ChainBase.java:190)
at mons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
at mons.chain.impl.ChainBase.execute(ChainBase.java:190)
at org.apache.posableRequestProcessor.process(ComposableRequestProcessor.java:283)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1913)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:462)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache..JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:619)