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?