所以我正在努力实现以下目标,但我不知道如何实现。
$.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: '+data.myVar); // <<<< data.myVar is not accessible from here
console.log('the value of myVar was: '+myVar); // <<<< myVar is not accessible from here
}
});
有没有办法在 .success()
函数中访问 myVar
的值?
我能否以某种方式在 .success()
函数中获取在此 ajax 请求中发送的原始 data
对象?
希望得到您的解决方案。 谢谢!
最佳答案
您可以使用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);
}
});
关于javascript - jQuery ajax 请求 : how to access sent data in success function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35385958/