[英]Jquery/JavaScript - Store Ajax jSONP response into variables
我正在使用JSONP獲得ajax請求的結果,沒有任何問題。 這是我的代碼
function TestJSONP()
{
$.ajax({
url: "https://www.sample.com/api/users.json?account_api_key=0000&unique_install_id=0000&email_address=test@test.com&locale_id=en-US&operating_system_version=6.1.7601.65536&operating_system_architecture=64&outlook_version=2013&version=0.0.5.0",
// the name of the callback parameter, as specified by the YQL service
jsonp: "callback",
// tell jQuery we're expecting JSONP
dataType: "jsonp",
// tell YQL what we want and that we want JSON
data: {
q: "select title,abstract,url from search.news where query=\"cat\"",
format: "json"
},
// work with the response
success: function (response) {
console.log(response); // server response
}
});
}
我需要將響應數據設置為我可以在該請求之外訪問的變量。 請指教。 (我讀了一些類似的問題,我無法將它們的解決方案應用於我的。因為我認為我的響應數據結構有點不同)請參閱下面的塊以查看console.log的結果(響應);
{
account:
{
id: "sadasdd4234",
name: "Sample Development",
support_email_address: "test1@sample.com",
report_threat_button_text: "text1",
successful_report_text: "text2",
false_report_text: "text3",
},
current_plugin_version: "0.0.1",
id: "trt45rety",
status: "ok",
type: "api_response",
user:
{
id: "erwrretV0",
language: "en",
first_name: "Robert",
last_name: "Croos",
email_address: "test2@sample.net"
}
}
提前致謝。 Kushan Randima
試試這個例子:
只需在函數外部聲明一個Global變量,並在ajax響應之后將響應變量分配給該全局變量。
var jsonData;
function TestJSONP()
{
$.ajax({
url: "https://www.sample.com/api/users.json?account_api_key=0000&unique_install_id=0000&email_address=test@test.com&locale_id=en-US&operating_system_version=6.1.7601.65536&operating_system_architecture=64&outlook_version=2013&version=0.0.5.0",
// the name of the callback parameter, as specified by the YQL service
jsonp: "callback",
// tell jQuery we're expecting JSONP
dataType: "jsonp",
// tell YQL what we want and that we want JSON
data: {
q: "select title,abstract,url from search.news where query=\"cat\"",
format: "json"
},
// work with the response
success: function (response) {
console.log(response); // server response
jsonData = response; // you can use jsonData variable in outside of the function
}
});
}
我嘗試驗證json響應,它似乎無效,可能這就是你無法將其設置為變量的原因。 您可以在http://jsonlint.com/上驗證json響應。
一旦糾正了json響應,就可以定義函數范圍之外的變量,並可以將響應分配給變量。 確保在函數之前定義變量。
var responseObject ;
function TestJSONP(){
.....
.....
// work with the response
success: function (response) {
responseObject = JSON.parse(response);
}
希望這可以幫助。
艾米的回答是正確的。 做得好! 我將用更多細節重新編寫它。 這對初學者很有幫助。
var jasonData;
function TestJSONP()
{
$.ajax({
url: "https://www.sample.com/api/users.json?account_api_key=0000&unique_install_id=0000&email_address=test@test.com&locale_id=en-US&operating_system_version=6.1.7601.65536&operating_system_architecture=64&outlook_version=2013&version=0.0.5.0",
// the name of the callback parameter, as specified by the YQL service
jsonp: "callback",
// tell jQuery we're expecting JSONP
dataType: "jsonp",
// tell YQL what we want and that we want JSON
data: {
q: "select title,abstract,url from search.news where query=\"cat\"",
format: "json"
},
// work with the response
success: function (response) {
console.log(response); // server response
//Save Account Data
account_id = response.account.id;
name = response.account.name;
support_email_address = response.account.support_email_address;
report_threat_button_text = response.account.report_threat_button_text;
successful_report_text = response.account.successful_report_text;
false_report_text = response.account.false_report_text;
//Main Object Data
current_plugin_version = response.current_plugin_version;
id = response.id;
status = response.status;
type = response.type;
//Save User Data
user_id = response.user.id;
language = response.user.language;
first_name = response.user.first_name;
last_name = response.user.last_name;
email_address = response.user.email_address;
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.