Link to home
Start Free TrialLog in
Avatar of Johny Bravo
Johny Bravo

asked on

Help with Jquery/Json

Hi Experts,

I am building a quiz system.

I have implemented the quiz referring below,
http://www.meredithdodge.com/2012/03/16/tutorial-easy-jquery-based-quiz/

This works on my machine very well, but on production server it is throwing error,

//Load json file
function getData(update) {

    alert(jsonpath + jsonfile);
    $.getJSON(jsonpath + jsonfile, function (json) {
        //Execute the callback
        alert("in");
        update(json);
    }).error(function () { alert("error"); });
}


It given alert here,     alert(jsonpath + jsonfile);
but then it alerts "error"

Why it is not working on production. Also on blog there are comments that it is not working on server.

Is there something to change on production?
Your assistance is much appreciated. Thanks
Avatar of Monica P
Monica P
Flag of India image

if you want to know Error message then you can use Firebug in firefox or

Hit F12 or Ctrl+Shift+I Then go to Console tab to check for the Errors
Avatar of Johny Bravo
Johny Bravo

ASKER

Hi leakim971,

Cross browser scripting... hmm well may be I am not getting the point, but my js and page is on same site.

For reference I am giving the js file below

/*!
* Easy jQuery-Based Quiz ~ Copyright (c) 2012 Meredith Dodge, http://meredithdodge.com
* Released under MIT license
*/

//The path to your images
var imgpath = "images/";
//The path to your json
var jsonpath = "json/";
//The filename will be determined by the rel attribute on div#quiz
var jsonfile = "";

$(document).ready(function () {
    //Make sure radio buttons are not disabled or checked (helpful when refreshing)
    $("input[type='radio']").attr("disabled", false);
    $("input[type='radio']").attr("checked", false);
    $(".submit").click(function (e) {
        e.preventDefault();
        //Check the quiz results
        checkQuiz();
    });
    //Build the json filename
    jsonfile = $("#quiz").attr("rel") + ".json";
});

//Load json file
function getData(update) {
    $.getJSON(jsonpath + jsonfile, function (json) {
        //Execute the callback
        update(json);
    }).error(function () { alert("error"); });
}

function checkQuiz() {
    $(".submit").remove();
    getData(function (data) {
        var ans = data.key;
        var result = {};
        $(".question").each(function () {
            //Get the question id
            var _q = $(this).attr("id");
            //Get the selected answer class
            var _a = $("#" + _q + " input:checked").closest("li").attr("class");
            //Add the values to the result object
            result[_q] = _a;
            //Indicate the correct answer
            $("#" + _q + " ." + ans[_q]).append("<img src='" + imgpath + "circle.png' class='png_bg' alt='correct answer'>");
            //Compare the selected answer with the correct answer
            if (ans[_q] == _a) {
                $(this).addClass("correct");
            } else {
                $(this).addClass("wrong");
            }
        });
        //Build the feedback
        var fdbck = "You got " + $(".correct").length + " out of " + $(".question").length + " correct. "
        if ($(".correct").length == 0) {
            fdbck += "Better luck next time.";
        } else if ($(".correct").length > $(".question").length / 2) {
            fdbck += "Good job!";
        } else {
            fdbck += "Not bad.";
        }
        $(".feedback").text(fdbck);
        $(".feedback").show();
    });
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi leakim971,

Thanks

replacing line now gives error "Error Not Found".
Is it like json is missing? while checking of ftp it is present at that location.

If I put the path in url like,
http://www.******.com/User/json/31.json
Getting error,

HTTP Error 404.3 - Not Found
The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

Any suggestion?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No I can't access json through url.
Getting error:
"HTTP Error 404.3 - Not Found
The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map."

Contacted to hosting team regarding, if it is an issue with mime type. Will update you.
Thanks
Are you calling this page in serverside script?? Other wise you should confirm the file location and extension. It can .js file or .json file.
Adding a MIME type for json on server resolves this issue.
Thanks a lot