mfadel
mfadel

Reputation: 897

.getJson is not filling the array

I have the following code:

$(document).ready(function() {
    $.getJSON('../includes/_quiz.php?class_id=163&course_id=183',function(data){
        $.each(data,function(k,v){
            questions[k] = v.question;
            answers[k*4] = v.ansO;
            answers[k*4+1] = v.ansT;
            answers[k*4+2] = v.ansTh;
            answers[k*4+3] = v.ansF;
            });

        //html  
        });
    });

the questions array is not being filled, yet when I debug, I get this result: when I request the page in the browser I receive the same result which is:

[{"question":"\u0623\u0647\u0645 \u0627\u0644\u0635\u0646\u0627\u0639\u0627\u062a \u0627\u0644\u0633\u0648\u0631\u064a\u0629 \u0648 \u062a\u062f\u062e\u0644 \u0628\u0627\u0644\u062a\u0635\u062f\u064a\u0631 :","ansO":"\u0627\u0644\u0623\u0633\u0645\u062f\u0629","ansT":"\u0627\u0644\u0623\u062f\u0648\u0627\u062a \u0627\u0644\u0632\u0631\u0627\u0639\u064a\u0629","ansTh":"\u0627\u0644\u0632\u064a\u0648\u062a \u0627\u0644\u0646\u0628\u0627\u062a\u064a\u0629","ansF":"\u0627\u0644\u0635\u0646\u0627\u0639\u0627\u062a \u0627\u0644\u0646\u0633\u064a\u062c\u064a\u0629","corr":"4"},{"question":"\u064a\u0633\u062a\u0648\u0631\u062f \u0627\u0644\u0639\u0631\u0627\u0642 \u0628\u0639\u0636 \u0627\u0644\u0645\u0648\u0627\u062f \u0645\u0646\u0647\u0627:","ansO":"\u0627\u0644\u0646\u0641\u0637","ansT":"\u0627\u0644\u062a\u0645\u0648\u0631","ansTh":"\u0627\u0644\u0623\u0644\u0627\u062a","ansF":"\u0627\u0644\u063a\u0627\u0632 \u0627\u0644\u0637\u0628\u064a\u0639\u064a","corr":"3"},{"question":"\u064a\u0628\u0644\u063a \u0639\u062f\u062f \u0633\u0643\u0627\u0646 \u0644\u064a\u0628\u064a\u0627 :","ansO":"\/3.5\/ \u0645\u0644\u064a\u0648\u0646 \u0646\u0633\u0645\u0629","ansT":"\/4\/ \u0645\u0644\u064a\u0648\u0646 \u0646\u0633\u0645\u0629","ansTh":"\/5\/ \u0645\u0644\u064a\u0648\u0646 \u0646\u0633\u0645\u0629","ansF":"\/5.5\/ \u0645\u0644\u064a\u0648\u0646 \u0646\u0633\u0645\u0629","corr":"4"}],

I debugged the code and discovered that it doesn't enter inside, it moves directly to the end

before I made some modifications to the code, the array was filled, what could be the problem?

Upvotes: 0

Views: 911

Answers (2)

Sandeep Manne
Sandeep Manne

Reputation: 6092

if the JSON file contains a syntax error, the request will usually fail silently. So add .error method and check if there is any error in parsing json

// Assign handlers immediately after making the request,
// and remember the jqxhr object for this request
var jqxhr = $.getJSON("example.json", function() {
  alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

Upvotes: 1

Mahipal
Mahipal

Reputation: 126

Please assign the data to one variable

Var result = data

you just write like

alert(result[0].questions)

you are getting an Array format but not assigned to any Variable. First assign to Variable

result = [{"question":"\u0623\u0647\u0645 \u0627\u0644\u0635\u0646\u0627\u0639\u0627\u062a \u0627\u0644\u0633\u0648\u0631\u064a\u0629 \u0648 \u062a\u062f\u062e\u0644 \u0628\u0627\u0644\u062a\u0635\u062f\u064a\u0631 :","ansO":"\u0627\u0644\u0623\u0633\u0645\u062f\u0629","ansT":"\u0627\u0644\u062‌​3\u062f\u0648\u0627\u062a \u0627\u0644\u0632\u0631\u0627\u0639\u064a\u0629","corr":"4"}]

Upvotes: 1

Related Questions