我有一个Javascript文件,我在其中使用Ajax XMLHttpRequest
来设置服务器中的数据并从中获取数据。现在,我有了以下与GET
功能一起使用的代码。现在,我要使用POST
而不是GET
。请帮助。我尝试了很多可能性,但没有任何效果。
function serverData(command, param) {
console.log("command is " + command + " param is " + param);
if (!command) {
console.log("Command not recieved, No call to Server");
return;
}
var url = "PasswordChangeSetting.cgi?command=" + command;
console.log("url is " + url);
if (param) url += "&" + param;
var xhttp = new XMLHttpRequest();
xhttp.open("GET", url, true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var data = JSON.parse(this.responseText);
alert("getting set resposne");
console.log("set repsonse is called" + command);
responseHandler(command, data);
}
};
}
function responseHandler(command, jsonData) {
if (!jsonData) {
console.log("Json from server not defined!!!");
return;
}
switch (command) {
case "set":
console.log("set repsonse is called");
if (jsonData.OUTPUT == "0") {
console.log("Policy committed successfully!");
document.getElementById("info").innerHTML = "Password chnaged..!!";
document.getElementById("info").style.color = "white";
}
if (jsonData.OUTPUT == 1) {
alert("ERROR: Password change failed");
console.log("Required Uppercase letter(s) is missing");
document.getElementById("info").innerHTML =
"ERROR: Required Uppercase letter(s) is missing";
//document.getElementById('info').innerHTML = jsonData.OUTPUT;
document.getElementById("info").style.color = "red";
}
if (jsonData.OUTPUT == 2) {
console.log("Required Lowercase letter(s) is missing");
document.getElementById("info").innerHTML =
"ERROR: Required Lowercase letter(s) is missing";
document.getElementById("info").innerHTML = jsonData.OUTPUT;
document.getElementById("info").style.color = "red";
}
break;
case "get":
console.log("get is called");
console.log("jsonData.LOWERCASE is " + jsonData.LOWERCASE);
document.getElementById("LC_range").innerHTML = jsonData.LOWERCASE;
console.log("jsonData.UPPERCASE is " + jsonData.UPPERCASE);
document.getElementById("UC_range").innerHTML = jsonData.UPPERCASE;
break;
default:
console.log("we don't support this");
}
}
答案 0 :(得分:0)
尝试以下代码:
var http = new XMLHttpRequest();
http.open('POST', url, true);
//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);