2016-02-13 70 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); 
    } 
}); 
相關問題