简体   繁体   English

访问对象属性-Ajax调用

[英]Access object properties - Ajax Call

I have simple AJAX call. 我有简单的AJAX通话。 It looks like this: 看起来像这样:

myApp.onPageInit('about', function (page) {

   search_term = $$("#search_term").val();
   const key = "xxxx-xxxx";
   const nick = "https://api.xxx.xxx/" + search_term + "?api_key=" + key;

   $$.ajax({
    url:nick,
    type:'GET',
    dataType: JSON,
    beforeSend: function(){
    //myApp.showPreloader('Please wait...');
    },
    success: function(data) {
        //myApp.hidePreloader();
        console.log(data);
        console.log(data['summonerLevel']);
    },
    statusCode: {
        404: function() {
           // myApp.alert('Account does not exist.', 'ERROR');
        }
      },
    error: function(data) {
        myApp.alert('Account does not exist.', 'ERROR');
        // ak je ovaj error, netko je dirao putanju - fajl 
    },

    }); 
})

When I'm trying to get properties from data object, every time it says undefined. 当我尝试从data对象获取属性时,每次它说未定义。

Console log: {"id":"xxxx","accountId":"xxxx","puuid":"xxxx","name":"Data","profileIconId":3015,"revisionDate":1546082318000,"summonerLevel":37} 控制台日志: {"id":"xxxx","accountId":"xxxx","puuid":"xxxx","name":"Data","profileIconId":3015,"revisionDate":1546082318000,"summonerLevel":37}

Second console log: undefined 第二个控制台日志: undefined

I tried dot notation and bracket notation, but everytime same status(undefined). 我尝试了点表示法和方括号表示法,但是每次都处于相同状态(未定义)。 Like this: 像这样:

console.log(data['summonerLevel']);
console.log(data.summonerLevel);

Any suggestion for this problem? 对这个问题有什么建议吗?

use JSON.Parse() method and try taking the value with object.attributename 使用JSON.Parse()方法,然后尝试使用object.attributename取值

$.ajax({
url:nick,
type:'GET',
dataType: JSON,
beforeSend: function(){
},
success: function(data) {
   var data=JSON.Parse(data);
    console.log(data.summonerLevel);
    console.log(data.accountId);
},
statusCode: {
    404: function() {
    }
  },
error: function(data) {

},

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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