Pass a variable to a function from inside an ajax call

后端 未结 2 640
鱼传尺愫
鱼传尺愫 2021-01-25 22:34

I tried to use this loop to read some urls to read their modified time:

var arr = [];

//... fill arr with push

for (var e in arr) {
        nodename=arr[e].hos         


        
2条回答
  •  Happy的楠姐
    2021-01-25 22:45

    your are directly executing the method and passing its result as the callback for the success callback.

    the xhr is already passed as the 3rd argument so try

    success: function(nn,status, xhr) {
                $('#host_'+nn).append("last modified: " + xhr.getResponseHeader("Last-Modified"));
            }
    

    if you have to pass the nodename as well, the you need to use a function that returns a function

    success: (function(nn){
                  return function(data ,status, xhr) {
                     // you can use nodename here...
                     $('#host_'+nn).append("last modified: " + xhr.getResponseHeader("Last-Modified"));
                };
            })(nodename)
    

提交回复
热议问题