我使用的是淘汰赛,我试图将信息发送到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')
接受它和其他任何我能想到的和没有结果。
有想法为什么删除行是问题? – Gerep 2012-02-15 11:45:34
如果您不将'data'参数作为对象传递,而是作为字符串传递,那么jQuery会假定您已经对其进行了URL编码,而您并未将其编码。但是,为了读取数据,PHP必须使用变量名进行URL编码。编写'data:{payload:ko.toJSON(this)}'告诉jQuery自动对JSON进行URL编码并将其放入'payload'变量中。 – wecsam 2012-02-16 02:25:50
对不起,我误解了你的问题。为了让PHP读取数据,您必须使用MIME类型'application/x-www-form-urlencoded'。 jQuery默认使用这个。指定'contentType:“application/json”'覆盖。 – wecsam 2012-02-16 02:28:40