Below code works on localhost, but not on live server.
MAIN EDIT:
Only 1 thing remains which is not working:
On AJAX
success this will being executed:
$(".FixedDiv").addClass("panel-danger"); setTimeout(close, 500); $("#label_" + res[2]).html(data.score_result); $("#monitoring_score").html(data.calculated_score);
How ever, the label
(for example) is not being updated. The label
needs to be updated by the score which is given (data.score_result).
Ajax code:
$('.rating').on('rating.change', function () { var rating_id = $(this).attr('id'); var res = rating_id.split("_"); var comment = $("#comments_" + res[2]).val(); var score = $("#item_score_" + res[2]).val(); var post = 'controller=QualityMonitoring&task=setScore&monitor_id=' + <?php echo $query['monitor_id']; ?> + '&q=' + res[2] + '&item_score=' + score + '&comment=' + comment; $.ajax({ url: "controller.php", type: "POST", data: post, cache: false, dataType: "json", beforeSend: function () { saveScore(); }, success: function (data) { $(".FixedDiv").addClass("panel-danger"); setTimeout(close, 500); $("#label_" + res[2]).html(data.score_result); $("#monitoring_score").html(data.calculated_score); } }); });
When I put alert('test');
above the $.ajax({
code it shows ‘test’. When I put the alert INSIDE (just below) the $.ajax({
code, it does not show the alert.
saveScore function:
function saveScore() { var docHeight = $(document).height(); $("body").append("<div id='overlay'></div>"); $("#overlay") .height(docHeight) .css({ 'opacity': 0.4, 'position': 'absolute', 'top': 0, 'left': 0, 'background-color': 'black', 'width': '100%', 'z-index': 5000 }); }
Results/info:
alert(post);
gives me the correct data result.saveScore
is executed, but won’t close afterwards (setTimeout)
.#label
and#monitoring_score
are not being updated like it has to do.- using
jquery-3.1.1
.
I’m distraught on how to solve this. Anyone has an idea on how to fix?
Extra:
@Teemu:
Add an error handler to the AJAX call too, most likely it’s the server-side which passes an error instead of data. Or open Network tab from the DevTools, and see if you’re actually getting 200 OK message and the data.
Edit 1: (Whole javascript code):
<script> $(document).ready(function () { $(".nav-tabs a").click(function () { $(this).tab('show'); }); }); $(document).ready(function () { $('.summernote').summernote({ height: 450, //set editable area's height toolbar: [ ['view', ['fullscreen']], ['help', ['help']] ], codemirror: { // codemirror options theme: 'monokai' } }); }); jQuery(document).ready(function () { $('.nvt').on('click', function () { // get the id: var id = $(this).attr('id'); var res = id.split("_"); // Reset rating: var rating_input = "item_score_" + res[1]; $('#' + rating_input).rating('update', 0); var comment = $("#comments_" + res[1]).val(); var score = 0; var post = 'controller=QualityMonitoring&task=setScore&monitor_id=' + <?php echo $query['monitor_id']; ?> +'&q=' + res[1] + '&item_score=' + score + '&comment=' + comment; $.ajax({ url: "controller.php", type: "POST", data: post, cache: false, dataType: "json", beforeSend: function () { saveScore(); }, success: function (data) { $(".FixedDiv").addClass("panel-danger"); setTimeout(closediv, 500); $("#label_" + res[1]).html(data.score_result); $("#monitoring_score").html(data.calculated_score); }, error: function (data) { $(".FixedDiv").addClass("panel-danger"); setTimeout(closediv, 500); $("#label_" + res[1]).html(data.score_result); $("#monitoring_score").html(data.calculated_score); } }); }); $('.rating').on('rating.change', function () { var rating_id = $(this).attr('id'); var res = rating_id.split("_"); var comment = $("#comments_" + res[2]).val(); var score = $("#item_score_" + res[2]).val(); var post = 'controller=QualityMonitoring&task=setScore&monitor_id=' + <?php echo $query['monitor_id']; ?> +'&q=' + res[2] + '&item_score=' + score + '&comment=' + comment; $.ajax({ url: "controller.php", type: "POST", data: post, cache: false, dataType: 'json', beforeSend: function (data) { saveScore(); }, success: function (data) { $(".FixedDiv").addClass("panel-danger"); setTimeout(closediv, 500); $("#label_" + res[2]).html(data.score_result); $("#monitoring_score").html(data.calculated_score); }, error: function(data) { console.log("ERROR: ", data); } }); }); $('.savecomment').on('blur', function () { var comment_id = $(this).attr('id'); var res = comment_id.split("_"); var commentraw = $("#comments_" + res[1]).val(); var comment = encodeURIComponent(commentraw); var post = 'controller=QualityMonitoring&task=setComment&monitor_id=' + <?php echo $query['monitor_id']; ?> +'&q=' + res[1] + '&comment=' + comment; $.ajax({ url: "controller.php", type: "POST", data: post, cache: false, dataType: "json", success: function (data) { if (data.result == 666) { $("#comments_" + res[1]).css("background-color", "#ffcccc"); } } }); }); }); $(document).on('change', '.btn-file :file', function () { var input = $(this), numFiles = input.get(0).files ? input.get(0).files.length : 1, label = input.val().replace(/\/g, '/').replace(/.*//, ''); input.trigger('fileselect', [numFiles, label]); }); $(document).ready(function () { $('.btn-file :file').on('fileselect', function (event, numFiles, label) { var input = $(this).parents('.input-group').find(':text'), log = numFiles > 1 ? numFiles + ' files selected' : label; if (input.length) { input.val(log); } else { if (log) alert(log); } }); }); function closediv() { $(document).unbind("keyup"); $("#overlay").fadeOut("slow", function () { $("#overlay").remove(); $(".FixedDiv").removeClass("panel-danger"); }); } function saveScore() { var docHeight = $(document).height(); $("body").append("<div id='overlay'></div>"); $("#overlay") .height(docHeight) .css({ 'opacity': 0.4, 'position': 'absolute', 'top': 0, 'left': 0, 'background-color': 'black', 'width': '100%', 'z-index': 5000 }); } $(document).ready(function () { var $sidebar = $(".FixedDiv"), $window = $(window), offset = $sidebar.offset(), topPadding = 55; $window.scroll(function () { if ($window.scrollTop() > offset.top) { $sidebar.stop().animate({ marginTop: $window.scrollTop() - offset.top + topPadding }); } else { $sidebar.stop().animate({ marginTop: 24 }); } }); }); </script>
Advertisement
Answer
Is your PHP code valid and not throwing extra code which is messing up your JSON object. When there is a notice the JSON object becomes a string instead of a JSON string and then javascript can’t parse it anymore.
Please make a new clean controller without any other code, post the data again and then check what is happening. Never return data but echo data with an exit.
Javascript and Code looks valid but somewhere else in your MVC may throw HTML code in the exit statement or generating it before you enter the controller which is required to return the data.