So i have an email sending function on my cordova app and it uses jQuery to do it. When debugging my app the ajax function works fine when testing in my browser, but when i build the app and test it on my phone it does not work. I had another problem like this that was only fixed once i used normal js instead of jQuery. Here is the function:
var message = localStorage.getItem("Message");
var key = "dJdJekCVAFIqvUJ13DEczZjgIh_4MyeIGEHz2GBYKFe"; // <<KEY
var message_name = "defender_send_message"; // <<MESSAGE NAME
var url = "https://maker.ifttt.com/trigger/" + message_name + "/with/key/" + key;
$.ajax({ // <<SEND
url: url,
data: {value1: message,
value2: localStorage.getItem("AdminsEmail")},
dataType: "jsonp",
complete: function(jqXHR, textStatus) {
console.log("Message Sent");
}
});
Does anyone know how to translate it into normal js? Thank you
You need to use XMLHttpRequest object to make a ajax call using vanilla JS.
var message = localStorage.getItem("Message");
var key = "dJdJekCVAFIqvUJ13DEczZjgIh_4MyeIGEHz2GBYKFe";
var message_name = "defender_send_message";
var url = "https://maker.ifttt.com/trigger/" + message_name + "/with/key/" + key;
var data = {};
data.value1 = message;
data.value2 = localStorage.getItem("AdminsEmail");
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
if (xmlhttp.status == 200) {
console.log("Message Sent");
}
}
}
xmlhttp.open('POST', url, true);
xmlhttp.responseType = 'jsonp';
xmlhttp.send(data);