Cordova - Ajax POST Request using vanialla js
You will learn how to do Ajax POST request using pure js with out some library like jquery .
If you google CORDOVA Ajax POST request,you will get results where jquery is used ,but you don't always need jquery right?
So,I tried making POST request, how we normally do using XMLHttpRequestand it worked like a charm with out adding any extra headers to the backend scripts as specified in most of the stackoverflows accepted articles.
JAVASCRIPT TO PERFORM AJAX POST REQUEST CORDOVA
var exampleData = JSON.stringify({
reqTime: new Date(),
user: 'xxxx'
});
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// perform action on responseText
// console.log(his.responseText);
}
}
xhttp.open("POST", "https://www.your-url.com", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(`exampleData=${exampleData}`);
Use above code as per your requirement.