【问题标题】:Apache Web server sending <html> tags for Json ResponseApache Web 服务器为 Json 响应发送 <html> 标签
【发布时间】:2020-11-01 02:43:53
【问题描述】:

我有一个简单的 Ajax 调用,例如

  $('#getOrgs').on('click', function() { 
        var form_data=new FormData(document.getElementsByName('f_xmlupdates')[0]);      
            $.ajax({
                url: 'ajax/getOrgs.php', 
                dataType: 'json', 
                contentType: false,
                processData: false,
                data: form_data,                       
                type: 'post',
                success: function(php_script_response){
                    $('#atscale_org').empty();
                    jQuery.each( php_script_response, function( i, val ) {
                        $('#atscale_org').append($('<option></option>').val(val[0]).html(val[1]));
                    });
                        $('#getOrgs').css('background','dimgrey');
                },
                    error: function ( error ) {
                        console.log("Something Wrong with GetOrgs..."+JSON.stringify(error));
                }
            });
});

PHP 端 - getOrgs.php

    <?php 
    header('Content-Type: application/json; charset=utf-8');
    include('../utils/utils.php');
    $organization = get_orgs_from_pg( $_POST["atscale_server"] );
    echo json_encode($organization,TRUE);
    exit();
    ?>

我使用的是 Apache WebServer 2.4.29。上面的代码填充了一个选择框。这在一天中的大部分时间都可以正常工作,有时(几天一次)Ajax 调用进入 Error 块而不是 Success 块。我看到 ServerState:4 错误消息。而且我看到 PHP 的响应带有以

开头的标签
<!DOCTYPE HTML>
<html>
<head>
</head> 
<body>
{json Response}
</body>
</html>

一旦我重新启动 Apache Web 服务器,问题就会消失。有人可以给我任何指示,哪个过程正在添加 HTML 标签?为什么会突然发生?

【问题讨论】:

  • 您只是说问题的原因,如果服务器端出现错误,那么您就不会得到 xml.... 在这种情况下,您可以解析该结果,就好像它不是纯 xml 一样。也许在出错时寻找正文标签来破译那里的 html/xml?
  • 也不知道这个get_orgs_from_pg() 做了什么。它就像一个黑匣子。无法调试。
  • 添加HTML标签的过程是服务器提供回退的默认内容,它被配置为在它无法识别请求时发回。
  • @GetSet function get_orgs_from_pg() 只从 postgres 返回几行 while ($row = pg_fetch_row($result)) { array_push($resp, array( htmlentities($row[0], ENT_XHTML) , htmlentities($row[1], ENT_XHTML), htmlentities($row[2], ENT_XHTML) )); } } 返回 $resp;是 htmlentities ,在 json 响应中添加那些 标记?我将删除 htmlentities 并尝试。
  • 当我再次遇到错误时,我会尝试删除 htmlentities 。现在 ajax 正在正确接收 json 响应。

标签: javascript php ajax apache webserver


【解决方案1】:

看起来问题出在我的 php 函数中的“htmlentities($row[2], ENT_XHTML) )”方法上。当我删除它时,我可以看到错误正在得到解决。谢谢大家的cmets!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    相关资源
    最近更新 更多