PHP接收Json

发布于 2025-01-06 08:18:12 字数 1581 浏览 1 评论 0原文

我正在使用淘汰赛,我正在尝试将信息发送到 PHP,使用 firebug 检查网络 ->标头我有这个:

Request URL:http://localhost/loyalty/welcome/json/
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:85
Content-Type:application/json
Host:localhost
Origin:http://localhost
Referer:http://localhost/loyalty/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11
X-Requested-With:XMLHttpRequest
Request Payload
{"friends":[{"name":"name","isOnTwitter":false},{"name":"name","isOnTwitter":false}]}
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Wed, 15 Feb 2012 11:01:23 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By:PHP/5.3.8

生成的 JSON 是: {"friends":[{"name":"name","isOnTwitter":false},{"name":"name","isOnTwitter":false} ]} 我不知道如何获取这些值。

这是 Ajax 调用:

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

在我的 CodeIgniter 方法上,我尝试使用 $this->input->post('friends') 以及我能想到的任何其他方法来接收它,但没有结果。

I'm using knockout and I'm trying to send information to PHP, using firebug to check Network -> Headers I have this:

Request URL:http://localhost/loyalty/welcome/json/
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:85
Content-Type:application/json
Host:localhost
Origin:http://localhost
Referer:http://localhost/loyalty/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11
X-Requested-With:XMLHttpRequest
Request Payload
{"friends":[{"name":"name","isOnTwitter":false},{"name":"name","isOnTwitter":false}]}
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Wed, 15 Feb 2012 11:01:23 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By:PHP/5.3.8

The generated JSON is: {"friends":[{"name":"name","isOnTwitter":false},{"name":"name","isOnTwitter":false}]} and I have no idea how to get those values.

Here is the Ajax call:

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

On my CodeIgniter method I have tried to receive it with $this->input->post('friends') and whatever else I could think of and no results.

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

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

发布评论

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

评论(4

天气好吗我好吗 2025-01-13 08:18:12

更改

data: ko.toJSON(this),

data: {mydata : ko.toJSON(this) },

在您在 http://localhost/loyalty/welcome/json/< 上发布时打开的文件中/a>

将其读为:

$myObj = json_decode($_POST['mydata']);

然后您可以访问您的值:

echo $myObj['friends'][0]['name']; or echo $myObj['friends'][0]['isOnTwitter'];

这将在您的 json 代码读取时输出 nametrue/false

编辑

该帖子应该对您有帮助 -> Jquery - 如何使 $.post() 使用 contentType=application /json?

change

data: ko.toJSON(this),

to

data: {mydata : ko.toJSON(this) },

In your file which opens up while you post on http://localhost/loyalty/welcome/json/

Read it as:

$myObj = json_decode($_POST['mydata']);

and then you can access your values as:

echo $myObj['friends'][0]['name']; or echo $myObj['friends'][0]['isOnTwitter'];

which will output name or true/false as your json code reads.

EDIT

This thread should help you -> Jquery - How to make $.post() use contentType=application/json?

看海 2025-01-13 08:18:12

我将 Javascript 更改为:

$.ajax({
    url:"http://localhost/loyalty/welcome/json/",
    type: "post",
    data: {payload:ko.toJSON(this)},
    success: function (result) {
            t.value = result;
    }
});

然后,在 PHP 中,您可以通过以下方式访问 JSON:

<?php json_decode($_POST["payload"]); ?>

I changed the Javascript to this:

$.ajax({
    url:"http://localhost/loyalty/welcome/json/",
    type: "post",
    data: {payload:ko.toJSON(this)},
    success: function (result) {
            t.value = result;
    }
});

Then, in PHP, you can access the JSON via:

<?php json_decode($_POST["payload"]); ?>
雪落纷纷 2025-01-13 08:18:12

由于 Content-Type 是“application/json”而不是“application/x-www-form-urlencoded”,因此 $_POST 数组将为空。

因此,为了访问 POST 请求中的 JSON 负载,您必须执行以下操作:

$json_data = json_decode(trim(file_get_contents('php://input')), true);    
echo($json_data['param1']);
echo($json_data['param2']);

As the Content-Type is "application/json" and NOT "application/x-www-form-urlencoded" the $_POST array will be empty.

So, in order to access the JSON payload in a POST request, you have to do this:

$json_data = json_decode(trim(file_get_contents('php://input')), true);    
echo($json_data['param1']);
echo($json_data['param2']);
物价感观 2025-01-13 08:18:12

要使 PHP 获取 JSON 对象,您需要使用 json_decode()。

http://php.net/manual/en/function.json-decode.php

For PHP to get JSON objects you need to use json_decode().

http://php.net/manual/en/function.json-decode.php

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