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

Jquery/JavaScript—将Ajax jSONP响应存储到变量中,javascript,jquery,ajax,variables,jsonp,Javascript,Jquery,Ajax,Variables,Jsonp,我使用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

我使用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(响应)的结果

提前谢谢。Kushan Randima

试试这个例子:

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

艾米的回答是正确的。干得好我会用更多的细节重新写。这对初学者很有帮助

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;
    }
});
}

我尝试验证json响应,但它似乎无效,这可能就是您无法将其设置为变量的原因。您可以在以下位置验证json响应

更正json响应后,可以在函数范围外定义一个变量,并将响应分配给该变量。确保在函数之前定义变量

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

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

希望这能有所帮助。

你到底想要什么?用一个变量来回答它。@Amy,谢谢你的提问。我找到了解决办法。这很简单。我将在几分钟后公布我的答案。你可以声明全局变量并为其分配响应变量。我刚刚发布了答案,如果它有用的话,请接受它,感谢它提供了验证JSON的链接。但这是关于“JSONP”(带填充的JSON)。我也可以用它来验证JSONP吗(尽管我自己还不能尝试),是的,你也可以验证JSONP。我所知道的唯一区别是JSONP响应来自不同的域,但是两者的格式是相同的。如果在格式上有任何差异,请让我知道,或者如果你知道
var responseObject ;
function TestJSONP(){
 .....
 .....

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