查詢了一下jquery的文檔,再看了一下網上的資料,自己試驗了一下,已經通過,代碼如下:
client js 代碼:
- <script type="text/javascript">
- function test() {
- varurl = "http://yourip/your_dir/your_action.action?id=1&jsoncallback=?";
- $.getJSON(
- url,
- function (Json) {
- alert(Json.UserID);
- alert(Json.UserName);
- });
- }
- </script>
server action 代碼:
- public void Test(){
- HttpServletResponse Response = ServletActionContext.getResponse();
- HttpServletRequest Request = ServletActionContext.getRequest();
- String jsoncallback = Request.getParameter("jsoncallback");
- String id = Request.getParameter("id");
- System.out.println(id + " @ " + jsoncallback);
- if (jsoncallback != null)
- {
- jsoncallback= (jsoncallback +"({\"UserID\":\"1234567890\",\"UserName\":\"admin\"})");
- }
- try {
- PrintWriter out = Response.getWriter();
- out.flush();
- out.println(jsoncallback);
- out.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
注意點,url後的“&jsoncallback=?”是不可以改變的,用到這塊的可以試一下,有問題可以留言,以上代碼俺已經測試通過