AJAX可在localhost上运行,但不能在实时服务器上运行 [英] AJAX works on localhost, but doesn't on live server

查看:79
本文介绍了AJAX可在localhost上运行,但不能在实时服务器上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可在localhost上运行,但不能在实时服务器上使用.

Below code works on localhost, but not on live server.

主要

仅剩一件事不起作用:

Only 1 thing remains which is not working:

成功执行AJAX时:

$(".FixedDiv").addClass("panel-danger");
setTimeout(close, 500);
$("#label_" + res[2]).html(data.score_result);
$("#monitoring_score").html(data.calculated_score);

但是,label(例如)没有被更新. label需要通过给定的分数(data.score_result)进行更新.

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代码:

$('.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);
    }
  });
});

当我将alert('test');放在$.ajax({代码上方时,它显示测试".当我将警报放在$.ajax({代码的下面(恰好在下面)时,它不显示警报.

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 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
    });
}

结果/信息:

  1. alert(post);给我正确的数据结果.
  2. saveScore已执行,但之后不会关闭(setTimeout).
  3. #label#monitoring_score并没有像需要的那样进行更新.
  4. 使用jquery-3.1.1.
  1. alert(post); gives me the correct data result.
  2. saveScore is executed, but won't close afterwards (setTimeout).
  3. #label and #monitoring_score are not being updated like it has to do.
  4. using jquery-3.1.1.

我对如何解决这个问题感到烦恼.任何人都有解决方法的想法吗?

I'm distraught on how to solve this. Anyone has an idea on how to fix?

其他:

@Teemu:

也向AJAX调用添加错误处理程序,很可能是 服务器端传递错误而不是数据.或打开网络"标签 从DevTools中查看,是否实际收到200 OK消息 和数据.

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.

(完整的javascript代码):

<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>

推荐答案

您的PHP代码是否有效,并且没有抛出多余的代码,这使您的JSON对象混乱.当有通知时,JSON对象变为字符串,而不是JSON字符串,然后javascript无法对其进行解析.

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和代码看起来有效,但是在您进入返回数据所需的控制器之前,MVC中的其他地方可能会在退出语句中生成HTML代码或生成HTML代码.

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.

这篇关于AJAX可在localhost上运行,但不能在实时服务器上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆