/** * DoPost, just does a post call * and passes the response to a * callback * * @param {string} url * @param {Object} json * @param {Function} callback * @param {Array} headers */ function doPost(url, json, callback, headers){ var xhr = new XMLHttpRequest(); xhr.open("POST", url, true); //Send the proper header information along with the request xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); if(typeof headers != 'undefined') for(var header in headers) xhr.setRequestHeader(header, headers[header]); xhr.onreadystatechange = function() { // Call a function when the state changes. if (this.readyState === XMLHttpRequest.DONE && this.status === 200) { console.log('____response___',this) console.log('this.responseText',this.responseText) var json = null; if(this.responseText) try { json = JSON.parse(this.responseText); } catch(err) { json = { response: this.responseText }; } callback(json); } if (this.readyState === XMLHttpRequest.DONE && this.status != 200) { console.log('____response___',this) console.log('this.responseText',this.responseText) var json = null; if(this.responseText) try { json = JSON.parse(this.responseText); } catch(err) { json = { response: this.responseText }; } callback(json); } console.log('this.readyState',this.readyState, this.status, this) } var data = encodeURI(Object.keys(json).map(function(k) { return k+'='+json[k]; }).join('&')); console.log('send data', data) xhr.send(data); }