Solved

Help with Jquery/Json

Posted on 2013-06-22
11
505 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 10

Expert Comment

by:Monica P
ID: 39267825
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 8

Author Comment

by:johny_bravo1
ID: 39268303
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 82

Accepted Solution

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

Author Comment

by:johny_bravo1
ID: 39269257
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
 
LVL 15

Assisted Solution

by:Jagadishwor Dulal
Jagadishwor Dulal earned 250 total points
ID: 39270139
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
ID: 39270347
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
ID: 39270423
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
ID: 39270465
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39270513
0
 
LVL 8

Author Closing Comment

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

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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 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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

623 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