首页 > 网站 > WEB开发 > 正文

AJAX - $.get( url, [data], [success], [type] )-3

2024-04-27 15:18:46
字体:
来源:转载
供稿:网友

上一篇【http://blog.csdn.net/J080624/article/details/54948321】提到请求格式为$.get(url,queryString,function(data){}所解决的两种思路。

以及请求格式为$.get(url,{mydata:queryString},function(data){}的两种思路。

本篇讲一下下面格式的后台接收第三种思路:

queryString = $('#editform').serializeArray();queryString = JSON.stringify(queryString);//string$.get(url,{mydata:queryString},function(data){}

【后台接收方法】

思路 :使用request.getParameter("key");

package com.atgui.Ajax.app.servlet;import java.io.IOException;import java.io.PRintWriter;import java.net.URLDecoder;import java.util.HashMap;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.codehaus.jackson.map.ObjectMapper;public class GetServlet3 extends HttpServlet { /** * queryString = $('#editform').serializeArray(); queryString = JSON.stringify(queryString);//string $.get(url,{mydata:queryString},function(data){}); */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); //拿到$("#editform").serialize(); String jsonString =request.getParameter("mydata") ; System.out.println(jsonString); ObjectMapper objectMapper = new ObjectMapper(); String resultJson = objectMapper.writeValueAsString(jsonString); System.out.println("resultJson ..."+resultJson); out.print(resultJson); out.flush(); out.close(); } public void init() throws ServletException { // Put your code here System.out.println("getServlet init()...."); }}

result as follows :

//启动servletgetServlet init()....//解析得到的mydata[{"name":"username","value":"yanshi02"},{"name":"passWord","value":"123456"}]//返回的jsonresultJson ..."[{/"name/":/"username/",/"value/":/"yanshi02/"},{/"name/":/"password/",/"value/":/"123456/"}]"

Tips:

参考post参考post传输参数数据格式为{key : value}可知:

无论 get 或者 post ,如果data为{key : value}形式。

则都可以使用request.getParameter("key");的形式进行获取。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表