PHP接收Json
我正在使用淘汰赛,我正在尝试将信息发送到 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更改
为
在您在 http://localhost/loyalty/welcome/json/< 上发布时打开的文件中/a>
将其读为:
然后您可以访问您的值:
这将在您的 json 代码读取时输出
name
或true/false
。编辑
该帖子应该对您有帮助 -> Jquery - 如何使 $.post() 使用 contentType=application /json?
change
to
In your file which opens up while you post on http://localhost/loyalty/welcome/json/
Read it as:
and then you can access your values as:
which will output
name
ortrue/false
as your json code reads.EDIT
This thread should help you -> Jquery - How to make $.post() use contentType=application/json?
我将 Javascript 更改为:
然后,在 PHP 中,您可以通过以下方式访问 JSON:
I changed the Javascript to this:
Then, in PHP, you can access the JSON via:
由于 Content-Type 是“application/json”而不是“application/x-www-form-urlencoded”,因此 $_POST 数组将为空。
因此,为了访问 POST 请求中的 JSON 负载,您必须执行以下操作:
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:
要使 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