Home >Web Front-end >JS Tutorial >jQuery study notes (1)--Using jQuery to implement asynchronous communication (using json to pass values) specific ideas_jquery

jQuery study notes (1)--Using jQuery to implement asynchronous communication (using json to pass values) specific ideas_jquery

WBOY
WBOYOriginal
2016-05-16 17:37:571072browse

jQuery is a popular js library nowadays, which can produce ideal effects with simple code, just like what the official website says "write less, do more". Jquery has rewritten the previous way of writing JavaScript to a certain extent. I use jquery to achieve the effect of using ajax to achieve asynchronous communication in the previous article, and feel the charm of jquery.

First you need to download the latest js file of jquery and introduce it into the file. You can also download it here: Click me to download.

This communication uses jquery’s jQuery.post(url,[data], [callback],[type]) method, this Is a simple POST request function to replace the complex $.ajax. The callback function can be called when the request is successful. The parameters are: url, [data], [callback], [type] and the corresponding parameter types are String, Map, Function, String:

url: Send request address.

data: Key/value parameters to be sent.

callback: Callback function when sending successfully.

type: Return content format, xml, html, script, json, text, _default)

Create a new jsp file jqueryDemo.jsp, the code is as follows:

Copy the code The code is as follows:

<%@ page language="java"contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>




jquery




























New user registration
User account: *

< /div>
User password: < input type="password"id="password1" name="password1">
Repeat password:




Create a new servlet file JqueryServlet.java, the code is as follows:

Copy the code The code is as follows:

package com.ldfsoft.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
/**
*Servlet implementation class JqueryServlet
*/
public class JqueryServlet extendsHttpServlet {
privatestatic final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public JqueryServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#service(HttpServletRequestrequest, HttpServletResponse response)
*/
protectedvoid service(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
//TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String account=request.getParameter("strAccount");
PrintWriter out=response.getWriter();
String str=""; //用以json传值
if(account.equals("admin")){
str="{success:true,msg:'该账户已存在'}";
}else{
str="{success:false,msg:'该账户可以使用'}";
}
out.write(str);
}
}

     好了,现在可以运行了,打开服务器,运行此jsp文件,页面如下所示:


    当输入admin时,页面如下所示:


     当输入其他的字符时,页面如下所示:


     可以看出jquery能够实现ajax的功能,并且代码更简洁了。

     只是,最后本人有一个问题迟迟没有解决,那就是输入中文时传到后台的值乱码,按照网上的好多办法都没有解决掉,不知道为什么,谁有更好的方法希望能给我推荐一下,本人不胜感激。

      这是本人学习的结果,允许转载,欢迎交流,但转载务必给出本文章的链接地址

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn