I created a standard function to POST a data object
post = function(url, data){
// Return a new promise.
return new Promise(function(resolve, reject) {
// Do the usual XHR stuff
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "application/json");
req.onload = function() {
// This is called even on 404 etc
// so check the status
if (req.status == 200) {
// Resolve the promise with the response text
resolve(req.response);
}
else {
// Otherwise reject with the status text
// which will hopefully be a meaningful error
reject(Error(req.statusText));
}
};
// Handle network errors
req.onerror = function() {
reject(Error("Network Error"));
};
var jayson = JSON.stringify(data);
req.send(jayson);
});
};
when I call this function with data = {key: value, anotherKey: value}, the jayson variable looks like this:
"{"key":value, "anotherKey":value}"
In the end, the data does not reach the server (the request does). I get a 200 status back, so on the server all is well.
(yes, it needs to be a POST - no, I do not want to use jQuery)
What's going wrong in my function, that it does not send my data?