I'm trying to create a function that will take object as a parameter and inside the object I want to have an object method just same way jQuery ajax works. My problem now is that I can't get that object method to work.
function myAjax({ type, url, success }) { var suc = ""; typeof type === "undefined" ? type = "GET" : type = type; var req = new XMLHttpRequest(); req.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { suc = this.responseText; //alert(this.responseText); } }; req.open(type, url, false); req.setRequestHeader("Content-type", "application/x-www-form-urlencoded "); req.send("fname=chijioke&lname=francis"); function success(suc) {} } myAjax({ type: "post", url: "page.html", success: function(e) { alert(e); } }); I want to do something similar to this jQuery ajax call
$.ajax({ type: "post", url: "index", data: { id: id }, success: function(feedback) { alert(feedback); } }); I need to get the responseText through the success function but it is not working. I want this to work like that of jQuery ajax success, this is really for learning purpose.
myAjaxfunction is never closed.