javascript - jQuery ajax 请求 : how to access sent data in success function?

标签 javascript jquery ajax

所以我正在努力实现以下目标,但我不知道如何实现。

$.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/

相关文章:

c# - 从 GeckoFX C# 控件调用 C# 方法 (winforms)

javascript - 当我点击该字母时获取该字母

javascript - 使用 toLocaleDateString 对相同代码产生不同结果

javascript - 为什么日期选择器不能在模式下工作?

javascript - 设置间隔为ajax形式

ajax - 如果在我们发出 Ajax 请求时互联网出现故障,那么如何向用户反馈互联网出现故障?

javascript - 如何使用 Angular JS 访问 JSON 中的嵌套对象

javascript - jQuery .nextAll() 不适用于 html 表列

php - 作为 JSON API 的 GUI 的 Chrome 扩展

javascript - 将数据从 Chrome 扩展发送到本地 python 文件