2012-02-15 52 views
1

我使用的是淘汰赛,我试图将信息发送到PHP,使用Firebug检查网络 - >头我有这样的:PHP接收的Json

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); 
      } 
     }); 

在我的笨方法,我试图用$this->input->post('friends')接受它和其他任何我能想到的和没有结果。

回答

2

我改变了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"]); ?> 
+0

有想法为什么删除行是问题? – Gerep 2012-02-15 11:45:34

+0

如果您不将'data'参数作为对象传递,而是作为字符串传递,那么jQuery会假定您已经对其进行了URL编码,而您并未将其编码。但是,为了读取数据,PHP必须使用变量名进行URL编码。编写'data:{payload:ko.toJSON(this)}'告诉jQuery自动对JSON进行URL编码并将其放入'payload'变量中。 – wecsam 2012-02-16 02:25:50

+2

对不起,我误解了你的问题。为了让PHP读取数据,您必须使用MIME类型'application/x-www-form-urlencoded'。 jQuery默认使用这个。指定'contentType:“application/json”'覆盖。 – wecsam 2012-02-16 02:28:40

3

变化

data: ko.toJSON(this), 

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

在你的文件,而你张贴在http://localhost/loyalty/welcome/json/

它读成打开了:

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

,然后你可以访问你的价值观是:

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

将输出nametrue/false为您的JSON代码读取。

编辑

这个线程可以帮助你 - >Jquery - How to make $.post() use contentType=application/json?

+0

它我布尔(假)使用笨,使用$ _ POST []它返回我“未定义返回变量:mydata“ – Gerep 2012-02-15 11:20:04

+0

你是否按照我在答案的开头提到的那样进行了更改? – linuxeasy 2012-02-15 11:21:03

+0

是的...这里是新的JSON:mydata = {“friends”:[{“name”:“João”,“isOnTwitter”:false}]}我的php和你的 – Gerep 2012-02-15 11:21:56

1

由于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']);