所以我试图实现以下目标,但我无法弄清楚如何使这项工作。
$.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
}
});
有没有办法在myVar
函数中访问.success()
的值?
我可以在data
函数中以某种方式获取此ajax请求中发送的原始.success()
对象吗?
希望您的解决方案。 谢谢!
答案 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)
首先将您要发布的数据存储在变量中。
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);
}
});
答案 2 :(得分: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);
答案 3 :(得分: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);
}
});
答案 4 :(得分: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);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="response"></p>
<p id="myVar"></p>
答案 5 :(得分: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
}
});
答案 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);
}
});