dongpin6941 2012-02-15 11:09
浏览 45
已采纳

PHP接收Json

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.

  • 写回答

4条回答 默认 最新

  • dstm2014 2012-02-15 11:29
    关注

    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"]); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站