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

前端之家收集整理的这篇文章主要介绍了AJAX - $.get( url, [data], [success], [type] )前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

success(response,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,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里面!!!


后台接收思路】:

讲post的时候已经说明,request.getReader()只能获取请求体(form data)的数据,URL的数据(query string)获取不到!

① 使用request.getParameterMap;

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

参考前篇post 的后台接收方法2:后台接收方法2
http://www.jb51.cc/article/p-omnjnxei-bpe.html

② 使用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,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=123456
resultJson ...{"username":"yanshi02","password":"123456"}

此时的json为typeof data = object 可以直接使用data.key 获取属性,不用进行转换!!!


题外话

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

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

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

请看下一篇JSON形式http://www.jb51.cc/article/p-olpzclwq-bpe.html

原文链接:https://www.f2er.com/ajax/161439.html

猜你在找的Ajax相关文章