2016-02-13 71 views
2

所以我试图实现以下,但我无法弄清楚如何使这项工作。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值的方法吗? 我可以以某种方式获取原始的data对象,该对象是在这个ajax请求中发送的,在.success()函数中?

希望得到您的解决方案。 谢谢!

+0

你测试只是console.log(myVar)? –

+0

@Gabriel Rodrigues是的,我做了,但没有奏效...... – Xriter

+0

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

回答

0

一般来说,如果你想能够多次引用数据,您需要确保它在正确的范围内。您在.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>

+1

有一件事我不明白:json'data'对象在'.ajax()'范围内。 '.success()'函数是'.ajax()'scope **以及**对不对?那么为什么我无法访问'.success()'函数中的'data'对象呢? – Xriter

+1

没关系,所以JSON对象作为'.ajax()'中的输入参数被匿名传入。假设我们将它定义为外部变量,但是。你不能控制'.ajax()'内部的逻辑,但是在某个点上它_calls_我们定义为'ajaxJSON.success()'的函数。并且它传递了'response'。 所以,你现在还没有真正处在'.ajax()'的范围内。你在'ajaxJSON.success()'的范围内。 这有帮助吗? –

+0

我明白了。谢谢! – Xriter

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); 
    } 
}); 
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); 
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); 
    } 
}); 
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); 
    } 
}); 
+0

为什么要将数据字符串化得更好? – powerc9000

+0

如果他想将数据发送给一个字符串(比如说因为后端期望它),那么这样他就可以轻松访问myData var,因为它是JS对象而不是原始字符串。 –

0

您可以使用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); 
    } 
}); 
相关问题