Joe Shenton Joe Shenton - 1 year ago 98
Ajax Question

Change callback function name

Hi so i have these two functions which

is a custom ajax request function and you can see that it has two parameters url works fine but the func one which is the callback doesn't seem to change func(serverResponse); to what
. So I was wondering what on earth am I doing wrong? It would be great if one of the kind developers on StackOverflow could help me out really soon. Thank you so much :)

function get(url, func) {
var xhReq = new XMLHttpRequest();"GET", url, false);
var serverResponse = xhReq.responseText;
func(serverResponse); // Shows "15"

get('ip.php', 'document.write');

Answer Source
get('ip.php', 'document.write');

should be:

get('ip.php', document.write.bind(document));

The former passes a string; the latter passes a function.

Perhaps a more typical way to do this sort of thing:

get('ip.php', function (text) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download