2014-10-31 89 views
1

我得到了使用JSONP的ajax請求的結果,沒有任何問題。這裏是我的代碼Jquery/JavaScript - 將Ajax jSONP響應存儲到變量中

function TestJSONP() 
    { 
    $.ajax({ 
     url: "https://www.sample.com/api/users.json?account_api_key=0000&unique_install_id=0000&[email protected]&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(response)的結果;

{ 
account: 
{ 
id: "sadasdd4234", 
name: "Sample Development", 
support_email_address: "[email protected]", 
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: "[email protected]" 
} 
} 

在此先感謝。貴霜Randima

+0

你想要什麼? – Amy 2014-10-31 06:07:26

+0

迴應它自己一個變量。 – Amy 2014-10-31 06:08:22

+0

@Amy,謝謝你的提問。我找到了解決方案。很簡單。我會在幾分鐘內發佈我的答案。仍在寫這個。 – 2014-10-31 06:10:53

回答

2

試試這個例子:

只是聲明瞭一個全局變量的函數外部和響應變量分配給Ajax響應後全局變量。

var jsonData; 
    function TestJSONP() 
    { 
    $.ajax({ 
     url: "https://www.sample.com/api/users.json?account_api_key=0000&unique_install_id=0000&[email protected]&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 
     } 
    }); 
} 
0

艾米的回答是正確的。做得好!我會重寫它的更多細節。這對初學者會有幫助。

var jasonData; 
    function TestJSONP() 
{ 
$.ajax({ 
    url: "https://www.sample.com/api/users.json?account_api_key=0000&unique_install_id=0000&[email protected]&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; 
    } 
}); 
} 
1

我試着驗證JSON響應,它似乎是無效的,可能這就是你無法將其設置爲變量的原因。您可以在http://jsonlint.com/上驗證json響應。

一旦你得到糾正的json響應,你可以定義一個超出函數範圍的變量,並且你可以給變量指定響應。確保變量在函數之前定義。

var responseObject ; 
function TestJSONP(){ 
..... 
..... 

// work with the response 
    success: function (response) { 
     responseObject = JSON.parse(response); 
} 

希望這有助於。

+0

感謝您提供驗證JSON的鏈接。但這是關於「JSONP」(帶填充的JSON)。我可以使用它來驗證JSONP(但我無法自己嘗試它) – 2014-10-31 06:30:56

+0

是的,你也可以驗證JSONP。我所知道的唯一區別是JSONP響應來自不同的域,但是兩者的格式相同。如果格式有任何不同,請告訴我,或者如果您知道 – anup 2014-10-31 06:59:09

+0

任何我可以參考的URL。謝謝 – anup 2014-10-31 07:00:27