ajax在函数外用数据,jQuery ajax请求:如何在成功函数中访问发送的数据?

7 个答案:

答案 0 :(得分:2)

您可以使用this来访问整个对象。所以你可以这样做:

$.ajax({

url: "whatever.php",

method: "POST",

data: { myVar: "hello" },

success: function(response) {

console.log('received this response: '+response);

console.log('the value of myVar was: '+this.data.myVar);

}

});

答案 1 :(得分:1)

除拼音的答案外,所有其他答案都是不可靠且不明智的做法。

就其本质而言,AJAX请求是异步的。这意味着,当响应从服务器返回时,它们设置的变量可能已更改。如果您想要一个示例,只需使两个按钮都触发相同的代码,但将myData设置为不同的值,然后在响应返回之前迅速单击它们,就可以了...现在变量已更改,您会得到不可靠的结果。

Piyin的答案也很好,但是有时您会收到不同格式的已发送数据。它可能是经过stringify处理的JSON对象,可能是带有查询字符串等的GET格式。

在编码器上最简单的方法(尽管它确实在RAM中增加了一些开销)是分配AJAX对象的新属性并在回调中访问它,如下所示(使用Piyin的示例):

var dataToSend = { myVar: "hello" };

$.ajax({

url: "whatever.php",

method: "POST",

data: dataToSend,

sentData: dataTosend, //add this property

success: function(response) {

console.log('received this response: ' + response);

console.log('the value of myVar was: '+ this.sentData.myVar); //access sentData property

}

});

答案 2 :(得分:1)

首先将您要发布的数据存储在变量中。

var data = {myVar: "hello"}

$.ajax({

url: "whatever.php",

method: "POST",

data: data,

success: function(response) {

console.log('received this response: '+response);

console.log('the value of myVar was: '+data.myVar);

}

});

答案 3 :(得分:1)

创建一个数据对象,以便您可以访问请求的各个部分

var postData ={ myVar: "hello" };

$.ajax({

url: "whatever.php",

method: "POST",

data: postData ,

success: function(response) {

console.log('received this response: '+response);

console.log('the value of myVar was: '+postData.myVar);

答案 4 :(得分:1)

将我的评论写成答案......

没有。无法从成功处理程序中访问$.ajax()的参数。您当然可以将该参数分配给更高范围的变量,然后传递该变量。然后,您将能够从success处理程序中访问该变量。

相反,你可以这样做:

var sentData = { myVar: "hello" };

$.ajax({

url: "whatever.php",

method: "POST",

data: sentData,

success: function(response) {

console.log('received this response: ' + response);

console.log('the value of myVar was: ' + sentData.myVar);

}

});

答案 5 :(得分:1)

通常,如果您希望能够多次引用数据,则需要确保它位于正确的范围中。您在.ajax()内传递的json数据对象的范围是ajax函数。如果您希望能够在其之外引用data:的值,例如您调用 .ajax()的范围,最简单的方法就是分配它变量。例如

myData = { myVar: "hello" };

$.ajax({

url: "whatever.php",

method: "POST",

data: myData,

success: function(response) {

console.log('received this response: '+response);

$("#response").html(response);

console.log('the value of myVar was: '+myData.myVar); // <<<< data.myVar is not accessible from here

$("#myVar").html(myData.myVar);

}

});

答案 6 :(得分:0)

您可以随时执行以下操作:

var myData = '{ myVar: "hello" }';

$.ajax({

url: "whatever.php",

method: "POST",

data: myData,

success: function(response) {

console.log('the value of myVar was: ' + myData.myVar);

}

});

甚至更好

var myData = {

myVar: "hello"

};

$.ajax({

url: "whatever.php",

method: "POST",

data: JSON.stringify(myData),

success: function(response) {

console.log('the value of myVar was: ' + myData.myVar);

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值