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

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?

图片转代码服务由CSDN问答提供 功能建议

我正在构建一个类似的输出数组

  if(  count($ errors)){
 $ success ='false';  
 $ output ['json_msg'] =“请再次尝试提交。”; 
 $ output ['errors'] = $ errors; 
} else {
 $ success ='true';  
 $ output ['json_msg'] =“感谢成为NOLA Insider!”; 
} 
 
 $ output ['success'] = $ success; 
 
 header('Content-type:application  / json; charset = utf-8'); 
 if(count($ errors)){http_response_code(500);  } 
 echo json_encode($ output);  
退出; 
   
 
 

但是,当我查看开发者工具的Chrome网络面板中的响应时,我看到响应中出现了新行:

我尝试在trim()中包装json_encode(),但这会产生乱码输出。

如何消除回车?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

相关推荐 更多相似问题