I'm getting the result of an ajax request using JSONP without any issues. Here is my code
function TestJSONP()
{
$.ajax({
url: ".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
}
});
}
I need to set the response data into variables which I can access outside that request. Please advice me. (I read some similar questions and I was not able to apply their solutions for mine. Because I think my response data structure is bit different) Please see following block to see the results of 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]"
}
}
Thanks in advance. Kushan Randima
I'm getting the result of an ajax request using JSONP without any issues. Here is my code
function TestJSONP()
{
$.ajax({
url: "https://www.sample./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
}
});
}
I need to set the response data into variables which I can access outside that request. Please advice me. (I read some similar questions and I was not able to apply their solutions for mine. Because I think my response data structure is bit different) Please see following block to see the results of 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]"
}
}
Thanks in advance. Kushan Randima
Share Improve this question edited Oct 31, 2014 at 6:11 Amy 4,0301 gold badge22 silver badges34 bronze badges asked Oct 31, 2014 at 5:50 Kushan RandimaKushan Randima 2,3005 gold badges38 silver badges63 bronze badges 6- What do you want exactly? – Amy Commented Oct 31, 2014 at 6:07
- response it self a variable. – Amy Commented Oct 31, 2014 at 6:08
- @Amy, Thanks for asking. I found the solution for that. It is simple. I will post my answer in few minutes. Still writing that. – Kushan Randima Commented Oct 31, 2014 at 6:10
- 1 Yup its is very simple you could just declare global variable and assign response variable to that. – Amy Commented Oct 31, 2014 at 6:11
- 1 I just posted the answer if it is useful accept it – Amy Commented Oct 31, 2014 at 6:15
3 Answers
Reset to default 2Try this example:
Just declare a Global variable outside of the function and assign response variable to that global variable after ajax response.
var jsonData;
function TestJSONP()
{
$.ajax({
url: "https://www.sample./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
}
});
}
I tried validating the json response and it appears to be invalid and probably thats the reason you are not able to set it to variables. You can validate the json response at http://jsonlint./.
Once you get the json response corrected , you can define a variable outside the scope of function and you can assign the response to the variable. ensure the variable is defined before the function.
var responseObject ;
function TestJSONP(){
.....
.....
// work with the response
success: function (response) {
responseObject = JSON.parse(response);
}
hope this helps.
Amy's answer is correct. Good Job! I will re-write it with more details. It will be helpful for a beginner.
var jasonData;
function TestJSONP()
{
$.ajax({
url: "https://www.sample./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;
}
});
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745331483a4622898.html
评论列表(0条)