PHP接收json

发布于 2025-01-05 17:16:06 字数 503 浏览 0 评论 0原文

我正在使用knockout,这是我的ajax代码:

save: function() {
                $.ajax({
                    url:"http://localhost/loyalty/welcome/json/",
                    type: "post",
                    data: ko.toJSON(this),
                    contentType: "application/json",
                    success: function (result) { alert(result) }
                });
            }

使用firebug我可以看到json消息已正确发送,问题是如何在PHP上接收它,已发送的消息的名称是什么?

我正在使用 CodeIgniter

提前感谢您的帮助。

I'm using knockout and this is my ajax code:

save: function() {
                $.ajax({
                    url:"http://localhost/loyalty/welcome/json/",
                    type: "post",
                    data: ko.toJSON(this),
                    contentType: "application/json",
                    success: function (result) { alert(result) }
                });
            }

Using firebug I can see that the json message is sent correctly, the problem is how to receive it on PHP, what is the name of what has been sent?

I'm using CodeIgniter

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音盲 2025-01-12 17:16:06

它位于变量 $_POST['key'] 中,其中 'key' 是 JSON 对象中的键值。

It would be in the variable $_POST['key'] where 'key' is the key values in the JSON object.

萌能量女王 2025-01-12 17:16:06
    **This is what exactly the way to post as json way**

//index.php
     $(document).ready(function(){
               obj = {}
               obj.name = "sam"
               obj.value = "12345"
                      $.ajax({
                               url:"json.php",
                               type: "post",
                               data :obj,
                               dataType:"json",
                               success: function (result) {
                                    alert(result.name);
                               }
                             });
            }); 

    //json.php  ,, the posted data is received as array ,, so we need to convert it as //json_encode to make as JSON again 

    <?php
    $jsonReceiveData = json_encode($_POST);
    echo $jsonReceiveData;
    ?>
    **This is what exactly the way to post as json way**

//index.php
     $(document).ready(function(){
               obj = {}
               obj.name = "sam"
               obj.value = "12345"
                      $.ajax({
                               url:"json.php",
                               type: "post",
                               data :obj,
                               dataType:"json",
                               success: function (result) {
                                    alert(result.name);
                               }
                             });
            }); 

    //json.php  ,, the posted data is received as array ,, so we need to convert it as //json_encode to make as JSON again 

    <?php
    $jsonReceiveData = json_encode($_POST);
    echo $jsonReceiveData;
    ?>
笑看君怀她人 2025-01-12 17:16:06
  save: function() {
            $.ajax({
                url:"http://localhost/loyalty/welcome/json/",
                type: "post",
                data: $(this).serialize()/*Where this is an instance of the form change with appropriate selector such as #loginForm*/,
                contentType: "application/json",
                success: function (result) { alert(result) }
            });
        }

在php文件中使用$_POST获取数据
我假设你也使用 jquery 并且 $ 是 jquery 函数。现在,这些数据可以在超级全球后获得。注意:您不需要使用 json 通过 ajax 函数发送数据。数据以序列化数组格式传递,例如: field1=value1&field2=value2 等...

如果您必须使用 json,坦率地说这是不必要的,请使用 data:"json="+ko.toJSON(form)

并在服务器上边数据=json_decode($_POST['json']);

  save: function() {
            $.ajax({
                url:"http://localhost/loyalty/welcome/json/",
                type: "post",
                data: $(this).serialize()/*Where this is an instance of the form change with appropriate selector such as #loginForm*/,
                contentType: "application/json",
                success: function (result) { alert(result) }
            });
        }

Use $_POST in the php file to get the data
I am assumin you are using jquery as well and $ is the jquery function. Now this data is available in the post superglobal. NB: you need not use json to send data through the ajax function. Data is passsed in a serialized array format like: field1=value1&field2=value2 etc...

If you must however use json, which frankly is unnecessary, use data:"json="+ko.toJSON(form)

and on server side data=json_decode($_POST['json']);

拧巴小姐 2025-01-12 17:16:06

解决方案是从ajax调用中获取

contentType: "application/json",

=)

The solution is to take

contentType: "application/json",

from ajax call.

=)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文