function - How is this called in JavaScript / Node? -
i'll post 2 quick examples , i'd know how these called
$.post('/path/to/file', {param1: 'value1'}, function(data, textstatus, xhr) { /*optional stuff after success */ });
in post function there's third parameter, function, data, textstatus , xhr available used within function, how called? use them time, feel still not understand "usage" or whole thing, mean, how can third parameter return or make 3 parameters of inner function available?
$.post
(along $.get
, $.ajax
etc) jquery's "sugar" on top of xmlhttprequest api. third argument, function, known callback, , called once request has completed request response argument(s). simplified version this:
function post(endpoint, params, callback) { var http = new xmlhttprequest(); var url = endpoint; var params = params; http.open("post", url, true); http.setrequestheader("content-type", "application/x-www-form-urlencoded"); http.onreadystatechange = function() { if (http.readystate == 4 && http.status == 200) { // callback called text (and other parameters) // passed in arguments // jquery returns textstatus, , jqxhr object // https://api.jquery.com/jquery.post/ callback(http.responsetext)); } } http.send(params); }
and it's used in same way $.post
.
post('http://example.com/post', {}, function (text) { console.log(text); });
Comments
Post a Comment