简体   繁体   English

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

[英]Jquery/JavaScript - Store Ajax jSONP response into variables

I'm getting the result of an ajax request using JSONP without any issues. 我正在使用JSONP获得ajax请求的结果,没有任何问题。 Here is my code 这是我的代码

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

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); (我读了一些类似的问题,我无法将它们的解决方案应用于我的。因为我认为我的响应数据结构有点不同)请参阅下面的块以查看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"
 }
}

Thanks in advance. 提前致谢。 Kushan Randima Kushan Randima

Try this example: 试试这个例子:

Just declare a Global variable outside of the function and assign response variable to that global variable after ajax response. 只需在函数外部声明一个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
        }
    });
}

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. 我尝试验证json响应,它似乎无效,可能这就是你无法将其设置为变量的原因。 You can validate the json response at http://jsonlint.com/ . 您可以在http://jsonlint.com/上验证json响应。

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. 一旦纠正了json响应,就可以定义函数范围之外的变量,并可以将响应分配给变量。 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.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM