【问题标题】:Json corrupt variable by addition of a character通过添加字符来破坏 Json 变量
【发布时间】:2020-11-19 16:28:18
【问题描述】:

我对服务器端 php 脚本进行 Ajax 调用。

php 应该返回一个类似的 json 变量

this : {"erreur":"Pas de spread possible avant le 20-11-2020","title":"Diffusion"}

我明白了: s{"erreur":"Pas de Diffusion possible avant le 20-11-2020","title":"Diffusion"}

这个's'从何而来?

我在服务器端的一段代码('DiffuseOffre.php'):

 $nextdate=date("d-m-Y", time() + 24 * 60 * 60);
    $message = "Pas de diffusion possible avant le " . $nextdate;
    $title = "Diffusion";
    $data_php = array(
        "erreur" => $message,
        "title" => $title
    );
    $retour = json_encode($data_php);
    $error=json_last_error();
    echo $retour;
    exit();

客户端的javascript:

function DiffuseOffre(envoi, tab, paquet, dest) {
    var server ='/Admin/Offres/DiffuseOffre.php';
    $.ajax({ 
        url: server,
        type:'Post',
        dataType: 'json',       
       data: {
            envoi: envoi,
            tab: tab,           
            paquet:paquet,
            dest: dest      
       }    
     }).done(function(response) {
        alert(response);        
       if (response.hasOwnProperty('erreur')) {
            $("#dialog-erreur").html(response.erreur);
            $("#dialog-erreur").dialog("open");
            $("#dialog-erreur").dialog({                
                width: '600px',
                title: response.title           
            });                 
       } else {     
                ....
        }
    }); 
}

php 调试器向我展示了 json 编码 $retour 的正确语法:

{"erreur":"Pas de diffusion possible avant le 20-11-2020","title":"Diffusion"}

在 Firefox 调试器上,“响应”显示为“未定义”并在控制台中:

Uncaught SyntaxError: unexpected token: identifier
    DiffuseOffre http://localhost/Admin/Util/js/main.js:336
    onclick http://localhost/Admin/Offres/diff_offre.php:1

我寻找一个粘性“s”,它本来可以粘贴到我的 $retour 或 $message 变量中,但什么也没有!

有人知道如何删除那个 's' 字符吗?

【问题讨论】:

    标签: php jquery json ajax


    【解决方案1】:

    您只是想找出您的 JSOn 文件有什么问题吗?如果是这样,您可以将其复制/粘贴 https://jsoneditoronline.org/# 并且您会立即看到该额外字符所在的位置。

    【讨论】:

      【解决方案2】:

      我终于明白了。 在我完整的 php 代码中,我包含了一个具有不同参数的文件。这也是一个php文件。 在最后一个结束标记 (?>) 之后,我找到了“s”字符。 由于它的位置(在 php 标签之外),它不会在我的 IDE 中生成任何语法错误。 这可能是缺少 Ctrl+s 的结果

      对我来说,这并不能解释为什么我发现它粘贴到返回值 (!)

      仅仅浪费了一天:-)

      对不起。

      【讨论】:

        猜你喜欢
        • 2014-07-04
        • 2019-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-18
        • 2015-10-12
        • 2021-03-15
        • 2011-03-24
        相关资源
        最近更新 更多