在处理PHP JSON请求 [英] handle json request in PHP

查看:125
本文介绍了在处理PHP JSON请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当调用Ajax,当时的contentType设置为应用程序/ JSON,而不是默认的X WWW的形式urlen codeD,服务器端(在PHP)不能得到这个职位的参数。
在下面工作的例子,如果我设置的contentType在Ajax请求应用/ JSON,PHP $ _ POST将是空的。为什么会发生这种情况?如何处理一个请求,其中的contentType为应用程序/ JSON正确的PHP?

   $阿贾克斯({
                    缓存:假的,
                    键入:POST,
                    网址:xxx.php
                    //的contentType:应用/ JSON
                    过程数据:真正的,
                    数据:{my_params:12​​3},
                    成功:函数(RES){

                    },
                    完成:功能(XMLHtt prequest,text_status){

                    }
                  });

解决方案

 < PHP
   后续代码var_dump(json_de code(的file_get_contents(PHP://输入')));
?>
 

When making an ajax call, when contentType is set to application/json instead of the default x-www-form-urlencoded, server side (in PHP) can't get the post parameters.
in the following working example, if I set the contentType to "application/json" in the ajax request, PHP $_POST would be empty. why does this happen? How can I handle a request where contentType is application/json properly in PHP?

     
   $.ajax({
                    cache: false,
                    type: "POST",
                    url: "xxx.php",
                    //contentType: "application/json",
                    processData: true,
                    data: {my_params:123},
                    success: function(res){

                    },
                    complete: function(XMLHttpRequest, text_status) {

                    }
                  });

解决方案

<?php
   var_dump(json_decode(file_get_contents('php://input')));
?>

这篇关于在处理PHP JSON请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆