duanchou6534 2016-10-10 16:35
浏览 653
已采纳

json_encode()输出中的换行符

I am building an output array like so

            if (count($errors)) {
                $success = 'false'; 
                $output['json_msg'] = "Please try your submission again.";
                $output['errors'] = $errors;
            } else {
                $success = 'true';  
                $output['json_msg'] = "Thanks for Becoming a NOLA Insider!";
            }

            $output['success'] = $success;

            header('Content-type:application/json;charset=utf-8');
            if (count($errors)) { http_response_code(500); }
            echo json_encode($output);          
            exit;

But when I look at the response in Chrome's Network pane of the developer tools I see what appears to be a newline in response:

developer console screenshot

I tried wrapping json_encode() in trim() but this gave garbled output.

How do I eliminate the carriage return?

  • 写回答

4条回答 默认 最新

  • doukuizuo1795 2016-10-10 17:07
    关注

    Do you have a ?> at the end of your PHP file and what's happening when you remove it ?

    Because you may have a carriage return at the end of the script which may be sent before your response :

    ?>
    
    // END OF FILE
    

    This is explained by the fact that PHP is actually a templating language :

    Here is a file which defines a function and which displays a text :

    <?php
    /**
     * @File lib.php
     */
     function sayHello()
     {
         echo "hello";
     }
     ?>
     forgotten text
    

    And here is a file that includes this file.

    <?php
    /**
     * @file index.php
     */
     include_once('lib.php');
     sayHello();
    

    This will output :

    forgotten text
    hello
    

    The "forgotten text" is output when the lib.php file is included whereas the "hello" is output after.

    (But it may be even simpler and just the point that @nanocv suggested)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办