AJAX亂碼與異步同步以及封裝jQuery庫實現(xiàn)步驟詳解
目錄

1、ajax亂碼問題

(1)發(fā)送ajax get 或者 ajax post請求時下面兩種情況?

①發(fā)送數(shù)據(jù)到服務(wù)器,服務(wù)器獲取的數(shù)據(jù)是否亂碼?

②服務(wù)器響應(yīng)給前端的中文,會不會亂碼?

(2)以tomcat9為例:

①前端代碼:填數(shù)據(jù),發(fā)送到服務(wù)器

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>測試ajax亂碼問題</title>
</head>
<body>
<script type="text/javascript">
    window.onload = function(){
        // -----------------------------------------------ajax get
        document.getelementbyid("btn1").onclick = function(){
            // 創(chuàng)建對象
            var xhr = new xmlhttprequest();
            // 注冊回調(diào)函數(shù)
            xhr.onreadystatechange = function (){
                if (this.readystate == 4) {
                    if (this.status == 200) {
                        document.getelementbyid("mydiv").innerhtml = this.responsetext
                    }
                }
            }
            // 打開通道
            var username = document.getelementbyid("username").value
            xhr.open("get", "/ajax/ajaxrequest7?username="+username+"&t=" + new date().gettime(), true)
            // 發(fā)送請求
            xhr.send()
        }
        // -----------------------------------------------ajax post
        document.getelementbyid("btn2").onclick = function(){
            // 創(chuàng)建對象
            var xhr = new xmlhttprequest();
            // 注冊回調(diào)函數(shù)
            xhr.onreadystatechange = function (){
                if (this.readystate == 4) {
                    if (this.status == 200) {
                        document.getelementbyid("mydiv").innerhtml = this.responsetext
                    }
                }
            }
            // 打開通道
            xhr.open("post", "/ajax/ajaxrequest7", true)
            xhr.setrequestheader("content-type", "application/x-www-form-urlencoded")
            var username = document.getelementbyid("username").value
            // 發(fā)送請求
            xhr.send("username=" + username)
        }
    }
</script>
<input type="text" id="username"><br>
<button id="btn1">發(fā)送ajax get請求,測試亂碼問題</button><br>
<button id="btn2">發(fā)送ajax post請求,測試亂碼問題</button><br>
<div id="mydiv"></div>
</body>
</html>

②后端代碼:先獲取數(shù)據(jù),然后在把數(shù)據(jù)響應(yīng)給服務(wù)器

package com.bjpowernode.javaweb.ajax;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
import java.io.printwriter;
/**
 * @program: 代碼
 * @classname: ajaxrequest7servlet
 * @version: 1.0
 * @description: 測試ajax亂碼問題
 **/
@webservlet("/ajaxrequest7")
public class ajaxrequest7servlet extends httpservlet {
    @override
    protected void doget(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        // 接收的中文會不會亂碼----不會
        string username = request.getparameter("username");
        system.out.println(username);
        // 響應(yīng)中文會有亂碼嗎?----會
         response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        out.print(username);
    }
    @override
    protected void dopost(httpservletrequest request, httpservletresponse response)
            throws servletexception, ioexception {
        // 接收的中文會不會亂碼----會
        request.setcharacterencoding("utf-8");
        string username = request.getparameter("username");
        system.out.println(username);
        // 響應(yīng)中文會有亂碼嗎?---會
         response.setcontenttype("text/html;charset=utf-8");
        printwriter out = response.getwriter();
        out.print(username);
    }
}

(3)測試結(jié)果:

對于tomcat10來說,關(guān)于字符集,我們程序員不需要干涉,不會出現(xiàn)亂碼。

對于tomcat9和之前的版本來說:

①對于get請求:接收前端的數(shù)據(jù)輸出到控制臺不會亂碼;把接收到的數(shù)據(jù)重新發(fā)給瀏覽器,輸出打印到瀏覽器會亂碼!

②對于post請求:接收前端的數(shù)據(jù)輸出到控制臺和把接收到的數(shù)據(jù)重新發(fā)給瀏覽器進行輸出兩者都會亂碼!

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频