javascript - Pass a variable to a function from inside an ajax call -
I tried to use this loop to read my url to read my modified time:
Var arr = []; // ... fill ur with push for push (var e in arr) {nodename = arr [e]. Hostname; Node_json = "/ nodes /" + nodename; Html + = '& lt; A href = "'+ node_json +'" target = "_ blank" id = "host _ '+ node name +'" & gt; Data & lt; / A & gt; '; Xhr = $ .ajax ({url: node_json, success: (function (nn) {$ ('# host_' + nn) .append ("Last modified:" + xhr.getResponseHeader ("last-modified")); }) (Node name)});
It does some work already, I comment on the success
line: I get calls on all node-files, and in firebug, I Many times in the header of different modified calls.
At first I was closed, (see) and only me got the last line modified with all the results. So I try to take action in a different function.
But it gives:
Reference: xhr is not defined
$ ('# host _' + Nn) .append ("Last modified:" + Xhr.getResponseHeader ("last-modified") ...
How do I get xhr
in that job?
I also tried:
... xhr [e] = $ .jg ({url: node_json, success: add_result_to_info (nodename, e)})}} // external loop Function add_result_to_info (nn, e) {$ ('# host_' + nn) .append ("Last Modified:" + xhr [e] .getResponseHeader ("Last-Modified"));}
Source of AJAX calls:
If arr
is actually an array, then just .forEach
or better .map
(old browsers Use with a shim), without the need to abort the extra, contains the scope of each repetition:
var xhrs = arr.map (function (e) {var name name = E.hostname; var node_json = "/ nodes /" + node name; Html + = '& lt; A href = "'+ node_json +'" target = "_ blank" id = "host _ '+ node name +'" & gt; Data & lt; / A & gt; '; Return ($ ('# host_' + node name). Append ("Last modified:" + xhr.getResponseHeader ("last-modified") ($ ('data, position, xhr)));); }); The reason for using var xhrs = arr.map ()
is the reason for using .forEach
is that you get (free) then Once every AJAX request has been completed once the ability to call another callback:
$ When.apply ($, xhrs) .then (function () {// woot! They all finished .. ..});
Comments
Post a Comment