我使用的是淘汰賽,我試圖將信息發送到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