Common problem solving methods in JavaScript: of garble IE cache proxy

  • 2020-03-30 00:03:07
  • OfStack

Two common ways to solve AJAX Chinese garbled code

1. EncodeURI on the client side (utf-8 is optional, by default), and iso-8859-1 encoding to utf-8 encoding on the server side

2. EncodeURI twice on the client and once on the server.

The second method can solve the problem:

Fix the IE cache problem
      Add a timestamp and check?

Solve the agency problem
      Would you want? To $

Sample code:


function verify() {
    //Method 1: encodeURI is used once for the data sent from the page end, and new String(old.getbytes ("iso8859-1")," utf-8 ") is used for the server segment.
    //Method 2: encodeURI is used twice for the data sent from the page end, and urldecoder.decode (old," utf-8 ") is used for the server segment.
    var url = "AJAXServer?name=" + encodeURI(encodeURI($("#userName").val()));
    url = convertURL(url);
    $.get(url,null,function(data){
        $("#result").html(data);
});
}
//Adding a timestamp to the url to fool the browser into not reading the cache
function convertURL(url) {
    //Get the timestamp
    var timstamp = (new Date()).valueOf();
    //Spliced the timestamp information onto the url
    //url = "AJAXServer"
    if (url.indexOf("?") >= 0) {
        url = url + "&t=" + timstamp;
    } else {
        url = url + "?t=" + timstamp;
    }
    return url;
}


Related articles: