【问题标题】:Receive JSON request from PHP接收来自 PHP 的 JSON 请求
【发布时间】:2012-10-10 16:04:53
【问题描述】:

在萤火虫中,在 POST 选项卡中,我看到以下内容;

JSON        

textfieldone "Alex"



Source
{"textfieldone :"Alex"}

但在 PARAMS 选项卡中我看到了

_dc 1341332451114

当我 print_r($_REQUEST); 我得到时,在我的 PHP 代码中

Array
(
    [_dc] => 1341332451114
)

而不是在 POST 选项卡中找到的 JSON。我该如何解决这个问题?

我不知道为什么会这样,我已经尝试调试了一整天

更新 PHP 代码:

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "pwd") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

print_r($_REQUEST);

在萤火虫中,我在 url 下看到了上述响应;

POST http://localhost/proj/php/result.php?_dc=1341332451114 200 OK 107ms

我可以知道?_dc=1341366375982 是什么吗?我正在发送 POST

更新 2

EXT JS4 代码

型号

Ext.define ('Mycomp.model.MyClass',{
    extend: 'Ext.data.Model',

    fields:['textfieldone']

});

查看

Ext.define('Mycomp.view.user.MyClassView', {
    extend: 'Ext.window.Window',
    alias: 'widget.myclassview',
    initComponent: function() {
        this.items = [
            {
                xtype: 'form',
                items: [
                    {
                        xtype: 'textfield',
                        name : 'textfieldone',
                        fieldLabel: 'Contact Person Name'
                    }
                ]
            }
        ];
        this.buttons = [
                        {
                            text: 'Save',
                            name:'save',
                            action: 'save'
                        }
                    ];
        this.callParent(arguments);
    }
});

控制器

Ext.define('Mycomp.controller.MyClass',{
    extend: 'Ext.app.Controller',

    stores:['MyClass'],
    models:['MyClass'],
    views:['MyClassView'],
    init: function(){
        this.control({          
            'myclassview button[action=save]': {
                click: this.myMethod
            }
        });         
        },
         myMethod: function(button, record) {

       var win    = button.up('window'),
           form   = win.down('form'), 
           values = form.getValues(),
           store = this.this.getmyClassStore(),
           model = store.model,
           record = model.create();


           record.set( values );
           store.add( record );
           win.close();
           store.sync(); 
}
});

商店

Ext.define('Mycomp.store.Myclass',{
    extend:'Ext.data.Store',
    model:'App.model.Myclass',

    proxy: {
        actionMethods : {
            create : 'POST'
        },
        type: 'ajax',
        url : '/savetodb.php'

    }

});

【问题讨论】:

  • 你确定它没有返回 404 吗?
  • 我在上面添加了我的 PHP 代码。我没有得到 404
  • 你是如何发送数据的,它们来自哪里?
  • 发布您正在发送帖子数据的 javascript
  • 还有什么是print_r($_POST)? POST 可能没有包含在 REQUEST 中?

标签: php json extjs4 firebug


【解决方案1】:

我想你会想要设置你的 .php 文件头来提供 JSON:

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

【讨论】:

  • 这是为了 sending JSON from PHP。问题是关于在 POST 请求中从客户端接收 JSON。
  • 小崽子——我迷路了。 @Illep 你能快速写下你有哪些文件以及它们的预期交互吗?
  • 我在上面的问题中包含了我的 Ext JS4 代码。我还对上面的 JSON POST 字符串进行了更改
  • 是的...我同意@andrewtweber - 我认为出于某种原因 $_REQUEST 只读取 $_GET 超全局中的内容...尝试: print_r($_POST) 在您的 PHP 文件中我想你会看到你的 textfieldone 信息。
  • 当我打印print_r($_POST) 我得到Array ( )
【解决方案2】:

试试下面的代码,应该可以解决你的问题:

<?php
   var_dump(json_decode(file_get_contents('php://input')));
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多