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

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

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

$.get(url,data,success(response,status,xhr),dataType)

url:必需。规定将请求发送的哪个 URL。

data:可选。规定连同请求发送到服务器的数据。

success(response,status,xhr):可选。规定当请求成功时运行的函数。

额外的参数:

response - 包含来自请求的结果数据​

status - 包含请求的状态​

xhr - 包含 xmlHttPRequest 对象 dataType:可选。规定预计的服务器响应的数据类型。

默认地,jQuery 将智能判断。

可能的类型:”xml”“html”“text”“script”“json”“jsonp”

jsp页面】

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'post.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/CSS" href="styles.css"> --> <script type="text/Javascript" src="./scripts/jquery-1.7.2.js"></script> <script type="text/javascript"> var url =""; var queryString = ""; $(function(){ $("input[type='button']").click(function(){ url = $("#editform").attr("action"); queryString = $('#editform').serialize(); $.get(url+"?querystring="+queryString,queryString,function(data){//querystring=username=yanshi02&password=123456&username=yanshi02&password=123456//resultJson ...{"username":"yanshi02","querystring":"username=yanshi02","password":"123456"} //$.get(url+"?querystring="+queryString,function(data){ //querystring=username=yanshi02&password=123456 //resultJson ...{"querystring":"username=yanshi02","password":"123456"} //$.get(url,queryString,function(data){ //username=yanshi02&password=123456 //resultJson ...{"username":"yanshi02","password":"123456"} alert(data); } ,"json"); }); }); </script> </head> <body> This is my JSP page. <br> <form id="editform" action="postServlet2" method="get"> <input type="text" name="username" value=""/> <input type="password" name="password" value=""/> </form> <input name="post" type="button" value="提交"/> </body></html>

如下图标明:

get方式 数据都在Query String Parameters里面!!!

这里写图片描述

【后台接收思路】:

① 使用request.getParameterMap;

该方法不分Form Data or Query String Parameters , 而是将数据以Map<String,String[]>返回。同名称的key对应的值,组成一个String[ ];

参考前篇post 的后台接收方法2:后台接收方法2 http://blog.csdn.net/j080624/article/details/54946974

② 使用request.getQueryString;

package com.atgui.Ajax.app.servlet;import java.io.IOException;import java.io.PrintWriter;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 GetServlet extends HttpServlet { /** * Destruction of the servlet. <br> */ 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.getQueryString() ; System.out.println(jsonString); //通过拿到的querystring转换为map HashMap<String, Object> map = getMapFromQueryString(jsonString); //将转换得到的map转换为json并返回 ObjectMapper objectMapper = new ObjectMapper(); String resultJson = objectMapper.writeValueAsString(map); 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()...."); } public HashMap<String, Object> getMapFromQueryString(String queryString) { //这个方法可能不适用你 HashMap map = new HashMap<String, String>(); String[] qStrings = queryString.split("&"); for (String string : qStrings) { String[] qStrings2 = string.split("="); map.put(qStrings2[0], qStrings2[1]); } return map; }}

result as follows :

getServlet init()....username=yanshi02&password=123456resultJson ...{"username":"yanshi02","password":"123456"}

题外话

上面所传输的数据格式为username=yanshi02&password=123456

如果传输的数据为JSON形式呢?

queryString = $('#editform').serializeArray();//objectqueryString = JSON.stringify(queryString);//string

请看下一篇JSON形式【http://blog.csdn.net/J080624/article/details/54949773】


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