Solved

Help with Jquery/Json

Posted on 2013-06-22
11
491 Views
Last Modified: 2013-06-24
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
0
Comment
Question by:johny_bravo1
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:Monica P
Comment Utility
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
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0
 
LVL 8

Author Comment

by:johny_bravo1
Comment Utility
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

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
Comment Utility
replace : }).error(function () { alert("error"); })
by : }).error(function (jqXHR, textStatus, errorThrown) { alert(textStatus + "\n" + errorThrown); });
0
 
LVL 8

Author Comment

by:johny_bravo1
Comment Utility
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?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 250 total points
Comment Utility
That is file not found error and you can check for your reference json file. Can you access your json file using url?? Manage the url.
0
 
LVL 8

Author Comment

by:johny_bravo1
Comment Utility
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
0
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
Comment Utility
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.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
0
 
LVL 8

Author Closing Comment

by:johny_bravo1
Comment Utility
Adding a MIME type for json on server resolves this issue.
Thanks a lot
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now