尝试将数组打印到控制台并分配给变量

时间:2017-10-17 15:26:49

标签: javascript jquery html

希望有人可以帮我解决这个问题。我正在使用包含数组的URL。我不知道为什么我不能打印到控制台。它在调试器选项卡中显示它正在工作。我很确定它有这样的事实,即数据不是json,它只是一个数组,但我需要使用JSONP,否则我得到了cors错误。

    // Where to Buy State Selection Options

var stateData = {};

$(document).ready(function() {

  "use strict";

  $("#mapWhereToBuy").usmap({ 
    click: function(event, data) { 
       var description = '';
       switch(data.name){
          case 'AL': 
              description = 'Alabama';
              break;
          case 'AK':
              description = 'Alaska';
              break;
          case 'AZ':
              description = 'Arizona';
              break;
          case 'AR':
              description = 'Arkansas'; 
              break;
          case 'CA':
              description = 'California';
              break;
          case 'CO':
              description = 'Colorado';
              break;
          case 'CT':
              description = 'Connecticut';
              break;
          case 'DC': 
              description = 'District of Columbia';
              break;
          case 'DE':
              description = 'Delaware';
              break;
          case 'FL':
              description = 'Florida';
              break;
          case 'GA': 
              description = 'Georgia';
              break;
          case 'HI':
              description = 'Hawaii';
              break;    
          case 'ID':
              description = 'Idaho';
              break;    
          case 'IL':
              description = 'Illinois';
              break;    
          case 'IN':
              description = 'Indiana';
              break;    
          case 'IA':
              description = 'Iowa';
              break;
          case 'KS':
              description = 'Kansas';
              break;
          case 'KY':
              description = 'Kentucky';
              break;
          case 'LA':
              description = 'Louisiana';
              break;
          case 'ME':
              description = 'Maine';
              break;
          case 'MD':
              description = 'Maryland';
              break;
          case 'MA':
              description = 'Massachusetts';
              break;
          case 'MI':
              description = 'Michigan';
              break;
          case 'MN':
              description = 'Minnesota';
              break;
          case 'MS':
              description = 'Mississippi';
              break;
          case 'MO':
              description = 'Missouri';
              break;
          case 'MT':
              description = 'Montana';
              break;
          case 'NE':
              description = 'Nebraska';
              break;
          case 'NV':
              description = 'Nevada';
              break;
          case 'NH':
              description = 'New Hampshire';
              break;
          case 'NJ':
              description = 'New Jersey'; 
              break;
          case 'NM':
              description = 'New Mexico'; 
              break;
          case 'NY':
              description = 'New York';
              break;
          case 'NC':
              description = 'North Carolina';
              break;
          case 'ND':
              description = 'North Dakota';
              break;
          case 'OH':
              description = 'Ohio';
              break;
          case 'OK':
              description = 'Oklahoma';
              break;
          case 'OR':
              description = 'Oregon';
              break;
          case 'PA':
              description = 'Pennsylvania';
              break;
          case 'RI':
              description = 'Rhode Island';
              break;
          case 'SC':
              description = 'South Carolina';
              break;
          case 'SD':
              description = 'South Dakota';
              break;
          case 'TN':
              description = 'Tennessee';
              break;
          case 'TX':
              description = 'Texas';
              break;
          case 'UT':
              description = 'Utah';
              break;
          case 'VT':
              description = 'Vermont';
              break;
          case 'VA':
              description = 'Virginia';
              break;
          case 'WA':
              description = 'Washington';
              break;
          case 'WV': 
              description = 'West Virginia';
              break;
          case 'WI':
              description = 'Wisconsin';
              break;  
          case 'WY':
              description = 'Wyoming';
              break;  
       }  

       $('#clicked-state').text(description); 
       console.log('called ' + data.name); 
       $.ajax({     
            dataType: "jsonp",    
            url: 'https://xxx.xxx.xx.xx:xxx/test/test/state/' + data.name,
            cache: true, 
            success: function(data){  
               stateData = JSON.stringify(data); 
               console.log(stateData);
            }
       });  

    } 
  });   

});

0 个答案:

没有答案