Javascript jQuery获取发送回的数据
使用jQuery.get方法时如何获取数据Javascript jQuery获取发送回的数据,javascript,jquery,ajax,Javascript,Jquery,Ajax,使用jQuery.get方法时如何获取数据 function send_data(pgId) { for(var i = 0; i < pgId.length; i++) { // $.get(url, data, success(data, textStatus, jqXHR)) $.get('index.php?page=' + pgId[i], pgId[i], function(respTxt, status, xhr)
function send_data(pgId)
{
for(var i = 0; i < pgId.length; i++)
{
// $.get(url, data, success(data, textStatus, jqXHR))
$.get('index.php?page=' + pgId[i], pgId[i], function(respTxt, status, xhr)
{
if(status === "success")
{
alert("Data received: " + respTxt + "\n");
alert("Data sent: " + pgId[i]); //<-- ???
}
});
}
}
函数发送数据(pgId)
{
对于(变量i=0;i 警报(“发送的数据:“+pgId[i]);//您可以使用一个闭包来执行此操作,该闭包将保留ID的值:
function send_data(pgId) {
var callbackWithId = function (pgId) {
//This will keep the pgId for the returned function
return function(respTxt, status, xhr) {
if(status === "success") {
alert("Data received: " + respTxt + "\n");
alert("Data sent: " + pgId);
}
}
}
for(var i = 0; i < pgId.length; i++) {
$.get('index.php?page=' + pgId[i], pgId[i], callbackWithId(pgId[i]));
}
}
函数发送数据(pgId){
var callbackWithId=函数(pgId){
//这将保留返回函数的pgId
返回函数(respTxt、状态、xhr){
如果(状态==“成功”){
警报(“收到的数据:“+respTxt+”\n”);
警报(“发送的数据:+pgId”);
}
}
}
对于(变量i=0;i