【问题标题】:Jquery/JavaScript - Store Ajax jSONP response into variablesJquery/JavaScript - 将 Ajax jSONP 响应存储到变量中
【发布时间】:2014-12-27 09:31:27
【问题描述】:

我正在使用 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(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"
 }
}

提前致谢。贵霜兰迪玛

【问题讨论】:

  • 你到底想要什么?
  • 自己响应一个变量。
  • @Amy,感谢您的提问。我找到了解决方案。很简单。我会在几分钟内发布我的答案。还在写。
  • 是的,它非常简单,您只需声明全局变量并将响应变量分配给它。
  • 我刚刚发布了答案,如果有用请接受它

标签: javascript jquery ajax variables jsonp


【解决方案1】:

试试这个例子:

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

【讨论】:

    【解决方案2】:

    艾米的回答是正确的。好工作!我会用更多的细节重写它。这对初学者会有帮助。

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

    【讨论】:

      【解决方案3】:

      我尝试验证 json 响应,但它似乎无效,这可能是您无法将其设置为变量的原因。您可以在 http://jsonlint.com/ 验证 json 响应。

      一旦你得到更正的 json 响应,你可以在函数范围之外定义一个变量,你可以将响应分配给变量。确保变量在函数之前定义。

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

      希望这会有所帮助。

      【讨论】:

      • 感谢您提供验证 JSON 的链接。但这是关于“JSONP”(带填充的 JSON)。我也可以用它来验证 JSONP 吗(我还是无法自己尝试)
      • 是的,您也可以验证 JSONP。我知道的唯一区别是 JSONP 响应来自不同的域,但是两者的格式相同。如果格式有任何差异,请告诉我或者如果您知道
      猜你喜欢
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      相关资源
      最近更新 更多