Ajax requests to send Chinese characters to the background and returns garbled codes.

first of all, in the first way, I encode at the front end, but the back end says that he does not cooperate with decoding, so this method cannot be solved.

    api.ewAjax = function(url, method, params, callback) {
    method = method.toUpperCase() === "GET" ? method : "POST";
    //  IE7+, Firefox, Chrome, Opera, Safari(XMLHttpRequest) 
    //  IE6, IE5(ActiveXObject)
    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    // GET or POST 
    if (method.toUpperCase() === "GET") {
        url = params ? url + "?" + params : url;
        xhr.open(method, url);
        xhr.send();
    } else {
        if (params) {
            if (typeof params === "string" && params.indexOf("=") !== -1) {
                xhr.open(method, url);
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
                xhr.send(params);
            } else {
                xhr.open(method, url);
                xhr.send(params)
            }
        } else {
            xhr.open(method, url);
            xhr.send()
        }
    }
    if (typeof callback === "function") {
        xhr.onreadystatechange = function(e) {
            if (xhr.readyState === 4 && xhr.status === 200) {
                callback(JSON.parse(xhr.responseText));
            }
        }
        xhr.onerror = function() {
            callback(this.status);
        }
    }
    // the chaining method 
    this.then = function(fn) {
        xhr.onreadystatechange = function(e) {
            if (xhr.readyState === 4 && xhr.status === 200) {
                fn.call(this, JSON.parse(xhr.responseText));
            }
        }
        xhr.onerror = function() {
            fn.call(this, this.status);
        }
    }
    return this;
};

I encapsulated it in native ajax.

the tomcat server used in the background, I feel dizzy. Is it my front-end fault or the back-end problem?

Apr.08,2022
If the

backend does not cooperate, then there is no solution to this problem! You encoded it. If he doesn't decode it, it must be garbled!
if this is not for encryption, it can also be obtained by directly transmitting it to the Chinese backend, and it is no problem to store it in the database.


there must be something wrong with this encapsulation. Why does post put the parameters into url?


Chinese transmission in the form of URL can only be encoded.
since it is POST, it must be transmitted in a data packet, ah, the common before and after interaction is transmitted in json format, there will be no problem of Chinese coding.
Why do you separate before and after even if you don't talk about the form of docking?


Chinese garbled code is easy to mess with, either the configuration file is unified, or it is set uniformly in the characters.

Menu