Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the two submission methods of ajax

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the two submission methods of ajax". In the daily operation, I believe many people have doubts about what the two submission methods of ajax are. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what are the two submission methods of ajax?" Next, please follow the editor to study!

First of all, it is mainly to comment on the javascript version of ajax: ajax asynchronous refresh is mainly to spell the required conditions into strings into the background, and after processing, directly call the callback function to return the obtained data to the page and display it. Because it is still on this page, there is no need to refresh the page. See, this article also uses encodeURI to encrypt the string and decode it in the class. Some of the things that need to be noted are annotated in the source code. There are two ways to submit get/post, but get submission is easy to garbled, so we must pay more attention to it.

Jsp page:

The copy code is as follows:

Var xmlHttp

Function createxmlHttpRequest () {

If (window.XMLHttpRequest) {

XmlHttp= new XMLHttpRequest (); / / IE7+,FireFox,Opera,Safari,Chrome

} else {

XmlHttp=new ActiveXObject ("Microsoft.XMLHTTP")

}

}

Function test () {/ / get

/ / get parameters

/ / var unames=encodeURI (document.getElementById ("username") .value); / / once encoding java is decoded by new String (name.getBytes ("ISO8859-1"), "UTF-8")

Var unames=encodeURI (encodeURI (document.getElementById ("username") .value); / / twice encoding to use java.net.URLDecoder.decode (name, "utf-8"); decode

Var pws=encodeURI (document.getElementById ("password") .value)

CreatexmlHttpRequest ()

XmlHttp.onreadystatechange=readyState

/ / function () {

/ / alert (xmlHttp.readyState+ "=" + xmlHttp.status); / / determine the request status

/ /}

XmlHttp.open ("get", "AjaxServlet1?msg=gets&name=" + unames+ "& pwd=" + pws+ "& timeStamp=" + new Date (). GetTime (), true); / / get will cause garbled code in Chinese, encodeURI () / encodeURIComponent () converts Chinese to hexadecimal coding, and encodes the string as URI

XmlHttp.send (null)

}

Function testp () {/ / post

/ / get parameters

Var unames=document.getElementById ("username"). Value

Var pws=document.getElementById ("password"). Value

CreatexmlHttpRequest ()

XmlHttp.onreadystatechange=readyState

XmlHttp.open ("post", "AjaxServlet1", true)

XmlHttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded")

Var str= "msg=posts&name=" + unames+ "& pwd=" + pws+ "& timeStamp=" + new Date () getTime ()

XmlHttp.send (str); / / send can be used to pass parameters

}

Function readyState () {

If (xmlHttp.readyState==4) {

If (xmlHttp.status==200) {

Var msg= xmlHttp.responseText

/ / alert (msg)

Document.getElementById ("result") [xss_clean] = msg

}

}

}

Js asynchronous refresh

Users:

Password:

Here is the servlet/action Java code:

The copy code is as follows:

Package com.cstp.javascript

Import java.io.IOException

Import java.io.PrintWriter

Import javax.servlet.ServletException

Import javax.servlet.http.HttpServlet

Import javax.servlet.http.HttpServletRequest

Import javax.servlet.http.HttpServletResponse

@ SuppressWarnings ("serial")

Public class AjaxServlet1 extends HttpServlet {

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

This.doPost (request, response)

}

Public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

/ / set the code to prevent garbled code

Response.setCharacterEncoding ("utf-8")

Request.setCharacterEncoding ("utf-8")

/ / receive parameters

String msg=request.getParameter ("msg")

If (msg.equals ("gets")) {

/ / String name=new String (request.getParameter ("name") .getBytes ("ISO8859-1"), "UTF-8"); / / once encoding, decoding operation in java

String name=java.net.URLDecoder.decode (request.getParameter ("name"), "utf-8"); / / the decode decoding page must be encoded twice, and the decoding operation is performed in java

String pwd=request.getParameter ("pwd")

System.out.println (name+ "," + pwd)

PrintWriter out = response.getWriter ()

Out.println ("ajax responds to get, the result returns" + name+ "," + pwd)

} else if (msg.equals ("posts")) {

String name=new String (request.getParameter ("name"). GetBytes ("utf-8"), "UTF-8"); / / once encoding, decoding operation in java

String pwd=request.getParameter ("pwd")

System.out.println (name+ "," + pwd)

PrintWriter out = response.getWriter ()

Out.println ("ajax responds to post, the result returns" + name+ "," + pwd)

}

}

}

Above is the javascript version of ajax, the following will like the jquery version to share with JQ friends:

On the page:

The copy code is as follows:

/ / method ①

Function circum (lon,lat) {

$.ajax ({

Url: "JQAjaxServlet?method=jsons"

ContentType: "application/x-www-form-urlencoded; charset=utf-8"

Type: 'post'

DataType: "json"

Async: false

Data: {/ / pass parameters to the backend

'lon': lon

'lat': lat

}

Success: function (data) {/ / return result via backend

Here, data returns data for the background, so you can deal with it as much as you like.

}

});

}

Background: in servlet/action

The method of data processing in the class is the same as above, so it is no longer cumbersome.

At this point, the study on "what are the two submission methods of ajax" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report