gpt4 book ai didi

javascript - Jquery/JavaScript - 将 Ajax jSONP 响应存储到变量中

转载 作者:行者123 更新时间:2023-11-30 12:34:06 24 4
gpt4 key购买 nike

我得到了使用 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
}
});
}

我需要将响应数据设置为我可以在该请求之外访问的变量。请给我建议。 (我读了一些类似的问题,但我无法将他们的解决方案应用到我的身上。因为我认为我的响应数据结构有点不同)请查看以下 block 以查看 console.log(response); 的结果。

{
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"
}
}

提前致谢。库山兰迪玛

最佳答案

试试这个例子:

只需在函数外声明一个全局变量,并在 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
}
});
}

关于javascript - Jquery/JavaScript - 将 Ajax jSONP 响应存储到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26668290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com