2015-07-10 66 views
1

如何將Java腳本中的ajax get函數參數與返回值一起傳出?JavaScript Pass函數參數與返回值一起

在下面的例子中,我想通過GetServer函數參數id的價值帶來並使其功能returnValue

function getServerList(id) { 
     $.ajax({ 
      type: "GET", 
      url: "/BackEndFunction/" + id, 
      contentType: "application/json; charset=utf-8", 
      dataType: "json", 
      success: returnValue 
     }); 
} 

function returnValue(Data) { 
    var _size = 0; 
    var id= id // passed from getserverlist 
    for (var i = 0; i< Data.length; i++) { 
     _size += Data[i]._size; 
    } 
    data_dictionary[id] = _size; 
} 
+0

使其可訪問性如何exa ctly? 'returnValue'接受一個參數嗎? –

+0

好點,錯過了,是的,總之,我會更新這個問題。 – JammAndTea

+0

你可以嘗試像thid這樣的東西:如果像returnValue(param_id,responce)那樣聲明'returnValue',所以你可以使用'bind'就像'success:returnValue.bind(this,id)' – Grundy

回答

3

匿名函數救援

function getServerList(id) { 
    $.ajax({ 
     type: "GET", 
     url: "/BackEndFunction/" + id, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function(data) { 
      returnValue(data, id); 
     } 
    }); 
} 
+0

從不使用匿名函數新的js - 函數(數據) - 那裏的數據是什麼?從Ajax調用中帶回的數據? – JammAndTea

+0

@JammAndTea:是的。 '$ .ajax'在收到響應時調用'success'回調函數,並將其作爲第一個參數傳遞給函數。 –

+0

F00kin'奇妙,謝謝你,先生,美好的一天!當它讓我時會接受。 – JammAndTea

1

您訪問也可以代理它

function getServerList(id) { 
    $.ajax({ 
     type: "GET", 
     url: "/BackEndFunction/" + id, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: $.proxy(returnValue, this, id) 
    }); 
}