php 服务器端如何接收Content-Type 为 application/json 为数据
微wx笑
2022-06-18【编程语言】
122
1
0关键字:
php json
php 服务器端如何接收Content-Type 为 application/json 为数据
php 服务器端如何接收Content-Type 为 application/json 为数据
客户端使用Ajax提交数据
传输类型为 POST
Content-Type 为 application/json
1 2 3 4 5 | var xhr = new XMLHttpRequest(); xhr.open( "POST" , "https://czsc.ivu4e.com/wu2198/xxx.php" , false ); xhr.setRequestHeader( "content-Type" , "application/json" ); xhr.send(JSON.stringify({name: "js" ,age: 18})); xhr.responseText |
服务器端直接使用 $_POST
是获取不到数据的
需要使用已下方法进行转化下
1 2 3 4 5 6 7 8 9 10 | <? //echo $_SERVER["CONTENT_TYPE"]; if ( empty ( $_POST ) && $_SERVER [ "CONTENT_TYPE" ] == "application/json" ) { $content = file_get_contents ( 'php://input' ); $_POST = json_decode( $content , true); } echo $_POST [ "name" ]; ?> |
本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/lang/2022-06-18/1255.html