尝试导入JSON文件时跳过$ .ajax函数

时间:2016-03-05 17:53:14

标签: javascript arrays json ajax

1)如果我尝试在Chrome中以调试模式运行,则会跳过此AJAX函数 2)当我正常运行时,AJAX工作,我得到长度为64的数组输出到控制台。但是在AJAX调用之后,相同的数组变空,长度变为0

我甚至尝试使用$(document).ready代替$("#nextQ").click(function(){

$("#nextQ").click(function(){ 

var Quotes = [];
var ID = [];
var Tag = [];
var seen = [];
//Get quotes from JSON file
  $.ajax({
                url: '../facts.json',
                datatype: 'json',
                type: 'get',
                success: function(data){
                                 console.log(data);
                                    console.log("successfully imported the JSON file");
                                 console.log(data.length); //Used to return 64
                                 totalQ = data.length; 
                                 for (i = 0; i < totalQ; i++){
                                           ID[i] = data[i][0];
                                           Tag[i] = data[i][1];
                                           Quotes[i] = data[i][2];
                                           seen[i] = data[i][3];
                            }
                                console.log(Quotes.length);
                    }
        });


//-----------------------
//This is where it all breaks down..the following totalQ is empty 
//

var Quote = new String;
var qnumber = 0;
var totalQ //The total number of available quotes to choose from
var Qrem //Remaining unseen quotes
var lock = new Boolean; //This will be true if the quote is already seen
var CurrentImage = String;

totalQ = Quotes.length - 1; //This length is returning 0 and totalQ = -1
console.log("TotalQ = " + totalQ);
ChooseQuote(0,totalQ);

1 个答案:

答案 0 :(得分:0)

尝试更换:

datatype: 'json',

使用:

dataType: 'json',

不要忘记javascript是一种区分大小写的编程语言。您还可以尝试向AJAX调用添加错误处理程序,以查看是否由于某种原因调用它。例如,如果远程端点返回一些无法解析的无效JSON,则会出现错误。