http request in https xmlpost java script code example
Example 1: xhr post send
var xhr = new XMLHttpRequest();
var params = 'field1='+postfield1+'&field2='+postfield2;
xhr.open('POST', 'http://exmaple.com', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function() {
if(xhr.status == 200) {
alert(this.responseText);
}
}
xhr.send(params);
Example 2: javascript http request
function _GET_REQUEST(url, response) {
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
response(this.responseText);
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
function _POST_REQUEST(url, params, response) {
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
response(this.responseText);
}
};
xhttp.open("POST", url, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(params);
}
_GET_REQUEST('http://url.com', (response) => {
console.log(response);
});
_POST_REQUEST('http://url.com', 'parameter=sometext', (response) => {
console.log(response);
});